本文整理汇总了Python中pulsar.apps.http.HttpClient.post方法的典型用法代码示例。如果您正苦于以下问题:Python HttpClient.post方法的具体用法?Python HttpClient.post怎么用?Python HttpClient.post使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pulsar.apps.http.HttpClient
的用法示例。
在下文中一共展示了HttpClient.post方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_login
# 需要导入模块: from pulsar.apps.http import HttpClient [as 别名]
# 或者: from pulsar.apps.http.HttpClient import post [as 别名]
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)
示例2: coveralls
# 需要导入模块: from pulsar.apps.http import HttpClient [as 别名]
# 或者: from pulsar.apps.http.HttpClient import post [as 别名]
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
示例3: minify
# 需要导入模块: from pulsar.apps.http import HttpClient [as 别名]
# 或者: from pulsar.apps.http.HttpClient import post [as 别名]
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()
示例4: testBadRequests
# 需要导入模块: from pulsar.apps.http import HttpClient [as 别名]
# 或者: from pulsar.apps.http.HttpClient import post [as 别名]
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)
示例5: coveralls
# 需要导入模块: from pulsar.apps.http import HttpClient [as 别名]
# 或者: from pulsar.apps.http.HttpClient import post [as 别名]
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:
from pulsar.apps.http import HttpClient
http = HttpClient(force_sync=True)
if not service_job_id:
service_job_id = os.environ.get('TRAVIS_JOB_ID', '')
if service_job_id:
service_name = 'travis-ci'
if not git:
git = gitrepo()
data = {
'service_job_id': service_job_id,
'service_name': service_name or 'pulsar',
'git': git,
'source_files': coverage.coveralls(strip_dirs, ignore_errors),
}
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)
info = response.json()
if 'error' in info:
stream.write('An error occured while sending coverage'
' report to coverall.io')
stream.write('\n%s' % info['message'])
示例6: Twitter
# 需要导入模块: from pulsar.apps.http import HttpClient [as 别名]
# 或者: from pulsar.apps.http.HttpClient import post [as 别名]
class Twitter(pulsar.Application):
'''A pulsar :class:`.Application` for connecting and streaming
tweets from `twitter streaming api`_.
This application requires the following parameters
to be specified in your ``config.py`` file:
* ``twitter_api_key`` the Consumer key of your application
* ``twitter_api_secret``, the Consumer secret
* ``twitter_access_token``, the application Access token
* ``twitter_access_secret``, the Access token secret
* ``twitter_stream_filter``, dictionary of parameters for
`filtering tweets`_.
'''
interval1 = 0
interval2 = 0
interval3 = 0
public_stream = 'https://stream.twitter.com/1.1/statuses/filter.json'
def monitor_start(self, monitor):
'''When the monitor starts make sure only one worker at most
is spawned
'''
self.cfg.set('workers', min(self.cfg.workers, 1))
def worker_start(self, worker, exc=None):
'''When the worker starts :meth:`connect` to twitter spreaming
endpoint
'''
api_key = self.get_param('twitter_api_key')
client_secret = self.get_param('twitter_api_secret')
access_token = self.get_param('twitter_access_token')
access_secret = self.get_param('twitter_access_secret')
self._http = HttpClient(encode_multipart=False)
oauth1 = OAuth1(api_key,
client_secret=client_secret,
resource_owner_key=access_token,
resource_owner_secret=access_secret)
self._http.bind_event('pre_request', oauth1)
self.buffer = []
self.connect()
@task
def connect(self):
'''Connect to twitter streaming endpoint.
If the connection is dropped, the :meth:`reconnect` method is invoked
according to twitter streaming connection policy_.
'''
filter = self.get_param('twitter_stream_filter')
return self._http.post(self.public_stream,
data=filter,
on_headers=self.connected,
data_processed=self.process_data,
post_request=self.reconnect)
def connected(self, response, **kw):
'''Callback when a succesful connection is made.
Reset reconnection intervals to 0
'''
if response.status_code == 200:
self.logger.info('Successfully connected with twitter streaming')
self.interval1 = 0
self.interval2 = 0
self.interval3 = 0
def process_data(self, response, **kw):
'''Callback passed to :class:`HttpClient` for processing
streaming data.
'''
if response.status_code == 200:
messages = []
data = response.recv_body()
while data:
idx = data.find(b'\r\n')
if idx < 0: # incomplete data - add to buffer
self.buffer.append(data)
data = None
else:
self.buffer.append(data[:idx])
data = data[idx+2:]
msg = b''.join(self.buffer)
self.buffer = []
if msg:
body = json.loads(msg.decode('utf-8'))
if 'disconnect' in body:
msg = body['disconnect']
self.logger.warning('Disconnecting (%d): %s',
msg['code'], msg['reason'])
elif 'warning' in body:
message = body['warning']['message']
self.logger.warning(message)
else:
messages.append(body)
if messages:
# a list of messages is available
if self.cfg.callable:
self.cfg.callable(self, messages)
def reconnect(self, response, exc=None):
#.........这里部分代码省略.........
示例7: Twitter
# 需要导入模块: from pulsar.apps.http import HttpClient [as 别名]
# 或者: from pulsar.apps.http.HttpClient import post [as 别名]
class Twitter(pulsar.Application):
'''This applications requires the following parameters
to be specified in your ``config.py`` file:
* ``twitter_api_key`` the Consumer key of your application
* ``twitter_api_secret``, the Consumer secret
* ``twitter_access_token``, the application Access token
* ``twitter_access_secret``, the Access token secret
* ``twitter_stream_filter``, dictionary of parameters for
filtering tweets.
Check the twitter stream filter api for further information
https://dev.twitter.com/docs/api/1.1/post/statuses/filter
'''
interval1 = 0
interval2 = 0
interval3 = 0
public_stream = 'https://stream.twitter.com/1.1/statuses/filter.json'
def monitor_start(self, monitor):
# this application has no workers
self.cfg.set('workers', 0)
api_key = self.get_param('twitter_api_key')
client_secret = self.get_param('twitter_api_secret')
access_token = self.get_param('twitter_access_token')
access_secret = self.get_param('twitter_access_secret')
self._http = HttpClient(encode_multipart=False)
oauth2 = OAuth1(api_key,
client_secret=client_secret,
resource_owner_key=access_token,
resource_owner_secret=access_secret)
self._http.bind_event('pre_request', oauth2)
self.buffer = []
self.connect()
def connect(self):
filter = self.get_param('twitter_stream_filter')
self._http.post(self.public_stream, data=filter,
on_headers=self.connected,
data_processed=self.process_data,
post_request=self.reconnect)
def connected(self, response, **kw):
if response.status_code == 200:
self.logger.info('Successfully connected with twitter streaming')
self.interval1 = 0
self.interval2 = 0
self.interval3 = 0
def process_data(self, response, **kw):
if response.status_code == 200:
messages = []
data = response.recv_body()
while data:
idx = data.find(b'\r\n')
if idx < 0:
self.buffer.append(data)
data = None
else:
self.buffer.append(data[:idx])
data = data[idx+2:]
msg = b''.join(self.buffer)
self.buffer = []
if msg:
body = json.loads(msg.decode('utf-8'))
if 'disconnect' in body:
msg = body['disconnect']
self.logger.warning('Disconnecting (%d): %s',
msg['code'], msg['reason'])
elif 'warning' in body:
message = body['warning']['message']
self.logger.warning(message)
else:
messages.append(body)
if messages:
# a list of messages is available
if self.cfg.callable:
self.cfg.callable(messages)
def reconnect(self, response, exc=None):
'''Handle reconnection according to twitter streaming policy
https://dev.twitter.com/docs/streaming-apis/connecting
'''
loop = response._loop
if response.status_code == 200:
gap = 0
elif not response.status_code:
# This is a network error, back off lineraly 250ms up to 16s
self.interval1 = gap = max(self.interval1+0.25, 16)
elif response.status_code == 420:
gap = 60 if not self.interval2 else max(2*self.interval2)
self.interval2 = gap
else:
gap = 5 if not self.interval3 else max(2*self.interval3, 320)
self.interval3 = gap
loop.call_later(gap, self.connect)
def get_param(self, name):
#.........这里部分代码省略.........