本文整理汇总了Python中pulsar.apps.http.HttpClient类的典型用法代码示例。如果您正苦于以下问题:Python HttpClient类的具体用法?Python HttpClient怎么用?Python HttpClient使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了HttpClient类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_graph
def test_graph(self):
c = HttpClient()
handler = Echo(c._loop)
ws = yield c.get(self.ws_uri, websocket_handler=handler)
self.assertEqual(ws.event('post_request').fired(), 0)
message = yield handler.get()
self.assertTrue(message)
示例2: test_login
def test_login(self):
http = HttpClient()
url = self.url + self.app.config['LOGIN_URL']
response = yield from http.get(url)
cookie = response.cookies.get('luxtest')
self.assertTrue(cookie)
self.assertEqual(response.status_code, 200)
doc = self.bs(response)
token = self.authenticity_token(doc)
self.assertEqual(len(token), 1)
# try to login
data = {'username': 'pippo', 'password': 'pluto'}
response2 = yield from http.post(url, data=data)
self.assertEqual(response2.status_code, 403)
#
# Add csrf token
data.update(token)
response2 = yield from http.post(url, data=data)
self.assertEqual(response2.status_code, 200)
cookie2 = response2.cookies.get('luxtest')
self.assertTrue(cookie2)
self.assertNotEqual(cookie2.value, cookie.value)
self.assertEqual(response2.headers['content-type'],
'application/json; charset=utf-8')
data = response2.json()
self.assertTrue('redirect' in data)
self.assertEqual(data['success'], True)
#
# Login again should cause MethodNotAllowed
response3 = yield from http.post(url, data=data)
self.assertEqual(response3.status_code, 405)
示例3: setup
def setup(self, http=None, timeout=None, **kw):
if not http:
timeout = timeout if timeout is not None else self.default_timeout
http = HttpClient(timeout=timeout, **kw)
http.headers['accept'] = 'application/json, text/*; q=0.5'
http.headers['content-type'] = 'application/json'
self.local.http = http
示例4: __init__
def __init__(
self,
url,
version=None,
data=None,
full_response=False,
http=None,
timeout=None,
sync=False,
loop=None,
encoding="ascii",
**kw
):
self.sync = sync
self._url = url
self._version = version or self.__class__.default_version
self._full_response = full_response
self._data = data if data is not None else {}
if not http:
timeout = timeout if timeout is not None else self.default_timeout
if sync and not loop:
loop = new_event_loop()
http = HttpClient(timeout=timeout, loop=loop, **kw)
http.headers["accept"] = "application/json, text/*; q=0.5"
http.headers["content-type"] = "application/json"
self._http = http
self._encoding = encoding
示例5: test_pong
def test_pong(self):
c = HttpClient()
handler = Echo()
ws = yield c.get(self.ws_echo, websocket_handler=handler).on_headers
#
ws.ping('TESTING CLIENT PING')
message = yield handler.get()
self.assertEqual(message, 'PONG: TESTING CLIENT PING')
示例6: test_verify
def test_verify(self):
c = HttpClient()
yield from self.async.assertRaises(SSLError, c.get, self.httpbin())
response = yield from c.get(self.httpbin(), verify=False)
self.assertEqual(response.status_code, 200)
response = yield from c.get(self.httpbin(), verify=crt)
self.assertEqual(response.status_code, 200)
self.assertEqual(response.request.verify, crt)
示例7: test_ping
def test_ping(self):
c = HttpClient()
handler = Echo()
ws = yield c.get(self.ws_echo, websocket_handler=handler).on_headers
#
# ASK THE SERVER TO SEND A PING FRAME
ws.write('send ping TESTING PING')
message = yield handler.get()
self.assertEqual(message, 'PING: TESTING PING')
示例8: coveralls
def coveralls(http=None, url=None, data_file=None, repo_token=None,
git=None, service_name=None, service_job_id=None,
strip_dirs=None, ignore_errors=False, stream=None):
'''Send a coverage report to coveralls.io.
:param http: optional http client
:param url: optional url to send data to. It defaults to ``coveralls``
api url.
:param data_file: optional data file to load coverage data from. By
default, coverage uses ``.coverage``.
:param repo_token: required when not submitting from travis.
https://coveralls.io/docs/api
'''
stream = stream or sys.stdout
coverage = Coverage(data_file=data_file)
coverage.load()
if http is None:
http = HttpClient(loop=new_event_loop())
if not git:
try:
git = gitrepo()
except Exception: # pragma nocover
pass
data = {'source_files': coverage.coveralls(strip_dirs=strip_dirs,
ignore_errors=ignore_errors)}
if git:
data['git'] = git
if os.environ.get('TRAVIS'):
data['service_name'] = service_name or 'travis-ci'
data['service_job_id'] = os.environ.get('TRAVIS_JOB_ID')
else:
assert repo_token, 'Requires repo_token if not submitting from travis'
if repo_token:
data['repo_token'] = repo_token
url = url or COVERALLS_URL
stream.write('Submitting coverage report to %s\n' % url)
response = http.post(url, files={'json_file': json.dumps(data)})
stream.write('Response code: %s\n' % response.status_code)
try:
info = response.json()
code = 0
if 'error' in info:
stream.write('An error occured while sending coverage'
' report to coverall.io')
code = 1
stream.write('\n%s\n' % info['message'])
except Exception:
code = 1
stream.write('Critical error %s\n' % response.status_code)
return code
示例9: minify
def minify(self, options, data):
b = convert_bytes(len(data))
self.write('Minimise %s css file via http://cssminifier.com' % b)
http = HttpClient(loop=new_event_loop())
response = http.post('http://cssminifier.com/raw',
data={'input': data})
if response.status_code == 200:
return native_str(response.get_content())
else:
response.raise_for_status()
示例10: test_dodgy_on_header_event
def test_dodgy_on_header_event(self):
client = HttpClient()
hook = partial(dodgyhook, self)
response = client.get(self.httpbin(), on_headers=hook)
try:
yield response.on_finished
except ValueError:
pass
self.assertTrue(response.headers)
self.assertIsInstance(response.on_headers.result, Failure)
示例11: test_close
def test_close(self):
c = HttpClient()
handler = Echo()
ws = yield c.get(self.ws_echo, websocket_handler=handler)
self.assertEqual(ws.event('post_request').fired(), 0)
ws.write('send close 1001')
message = yield handler.get()
self.assertEqual(message, 'CLOSE')
self.assertTrue(ws.close_reason)
self.assertEqual(ws.close_reason[0], 1001)
self.assertTrue(ws._connection.closed)
示例12: __init__
def __init__(self, url, version=None, data=None,
full_response=False, http=None, timeout=None, **kw):
self._url = url
self._version = version or self.__class__.default_version
self._full_response = full_response
self._data = data if data is not None else {}
if not http:
timeout = timeout if timeout is not None else self.default_timeout
http = HttpClient(timeout=timeout, **kw)
http.headers['accept'] = 'application/json, text/*; q=0.5'
http.headers['content-type'] = 'application/json'
self._http = http
示例13: test_home
def test_home(self):
http = HttpClient()
response = yield from http.get(self.url)
self.assertEqual(response.status_code, 200)
self.assertEqual(response.headers['content-type'],
'text/html; charset=utf-8')
cookie = response.cookies.get('luxtest')
self.assertTrue(cookie)
self.assertTrue(cookie.value)
response = yield from http.get(self.url)
cookie2 = response.cookies.get('luxtest')
self.assertFalse(cookie2)
示例14: test_close_sync
def test_close_sync(self):
loop = new_event_loop()
c = HttpClient(loop=loop)
handler = Echo(loop)
ws = c.get(self.ws_echo, websocket_handler=handler)
self.assertEqual(ws.event('post_request').fired(), 0)
self.assertEqual(ws._loop, loop)
self.assertFalse(ws._loop.is_running())
ws.write('send close 1001')
message = ws._loop.run_until_complete(handler.get())
self.assertEqual(message, 'CLOSE')
self.assertTrue(ws.close_reason)
self.assertEqual(ws.close_reason[0], 1001)
self.assertTrue(ws._connection.closed)
示例15: testBadRequests
def testBadRequests(self):
c = HttpClient()
response = yield c.post(self.ws_uri)
self.assertEqual(response.status_code, 405)
#
response = yield c.get(self.ws_uri,
headers=[('Sec-Websocket-Key', 'x')])
self.assertEqual(response.status_code, 400)
#
response = yield c.get(self.ws_uri,
headers=[('Sec-Websocket-Key', 'bla')])
self.assertEqual(response.status_code, 400)
#
response = yield c.get(self.ws_uri,
headers=[('Sec-Websocket-version', 'xxx')])
self.assertEqual(response.status_code, 400)