本文整理汇总了Python中http.client.BadStatusLine方法的典型用法代码示例。如果您正苦于以下问题:Python client.BadStatusLine方法的具体用法?Python client.BadStatusLine怎么用?Python client.BadStatusLine使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类http.client
的用法示例。
在下文中一共展示了client.BadStatusLine方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _http_req
# 需要导入模块: from http import client [as 别名]
# 或者: from http.client import BadStatusLine [as 别名]
def _http_req(self, method, path, payload=None, retries=2):
serialized_payload = json.dumps(payload) if payload is not None else None
try:
self._http.request(method, path, serialized_payload, self._COMMON_HEADERS)
http_response = self._http.getresponse()
except (http.BadStatusLine, http.CannotSendRequest):
self._http = http.HTTPConnection(self._host)
if retries > 0:
return self._http_req(method, path, payload, retries-1)
self._handle_error(self, None, Connection.OperationalError, "Connection has expired.")
if not http_response.status in [200, 201]:
message = "Server returned unexpected response: " + ustr(http_response.status) + ustr(http_response.read())
self._handle_error(self, None, Connection.OperationalError, message)
return http_response
示例2: test_status_lines
# 需要导入模块: from http import client [as 别名]
# 或者: from http.client import BadStatusLine [as 别名]
def test_status_lines(self):
# Test HTTP status lines
body = "HTTP/1.1 200 Ok\r\n\r\nText"
sock = FakeSocket(body)
resp = client.HTTPResponse(sock)
resp.begin()
self.assertEqual(resp.read(0), b'') # Issue #20007
self.assertFalse(resp.isclosed())
self.assertFalse(resp.closed)
self.assertEqual(resp.read(), b"Text")
self.assertTrue(resp.isclosed())
self.assertFalse(resp.closed)
resp.close()
self.assertTrue(resp.closed)
body = "HTTP/1.1 400.100 Not Ok\r\n\r\nText"
sock = FakeSocket(body)
resp = client.HTTPResponse(sock)
self.assertRaises(client.BadStatusLine, resp.begin)
示例3: retry_http
# 需要导入模块: from http import client [as 别名]
# 或者: from http.client import BadStatusLine [as 别名]
def retry_http( delays=default_delays, timeout=default_timeout, predicate=retryable_http_error ):
"""
>>> i = 0
>>> for attempt in retry_http(timeout=5): # doctest: +IGNORE_EXCEPTION_DETAIL
... with attempt:
... i += 1
... raise urllib.error.HTTPError('http://www.test.com', '408', 'some message', {}, None)
Traceback (most recent call last):
...
HTTPError: HTTP Error 408: some message
>>> i > 1
True
>>> i = 0
>>> for attempt in retry_http(timeout=5): # doctest: +IGNORE_EXCEPTION_DETAIL
... with attempt:
... i += 1
... raise BadStatusLine('sad-cloud.gif')
Traceback (most recent call last):
...
BadStatusLine: sad-cloud.gif
>>> i > 1
True
"""
return retry( delays=delays, timeout=timeout, predicate=predicate )
示例4: read_headers
# 需要导入模块: from http import client [as 别名]
# 或者: from http.client import BadStatusLine [as 别名]
def read_headers(self):
status_line = yield from self.getline()
if self.verbose: print('<', status_line, file=sys.stderr)
status_parts = status_line.split(None, 2)
if len(status_parts) != 3:
raise BadStatusLine(status_line)
self.http_version, status, self.reason = status_parts
self.status = int(status)
while True:
header_line = yield from self.getline()
if not header_line:
break
if self.verbose: print('<', header_line, file=sys.stderr)
# TODO: Continuation lines.
key, value = header_line.split(':', 1)
self.headers.append((key, value.strip()))
if self.verbose: print(file=sys.stderr)
示例5: read_headers
# 需要导入模块: from http import client [as 别名]
# 或者: from http.client import BadStatusLine [as 别名]
def read_headers(self):
"""Read the response status and the request headers."""
status_line = yield from self.getline()
status_parts = status_line.split(None, 2)
if len(status_parts) != 3:
self.log(0, 'bad status_line', repr(status_line))
raise BadStatusLine(status_line)
self.http_version, status, self.reason = status_parts
self.status = int(status)
while True:
header_line = yield from self.getline()
if not header_line:
break
# TODO: Continuation lines.
key, value = header_line.split(':', 1)
self.headers.append((key, value.strip()))
示例6: _request
# 需要导入模块: from http import client [as 别名]
# 或者: from http.client import BadStatusLine [as 别名]
def _request(self, method, path, postdata):
'''
Do a HTTP request, with retry if we get disconnected (e.g. due to a timeout).
This is a workaround for https://bugs.python.org/issue3566 which is fixed in Python 3.5.
'''
headers = {'Host': self.__url.hostname,
'User-Agent': USER_AGENT,
'Authorization': self.__auth_header,
'Content-type': 'application/json'}
try:
self.__conn.request(method, path, postdata, headers)
return self._get_response()
except httplib.BadStatusLine as e:
if e.line == "''": # if connection was closed, try again
self.__conn.close()
self.__conn.request(method, path, postdata, headers)
return self._get_response()
else:
raise
except (BrokenPipeError,ConnectionResetError):
# Python 3.5+ raises BrokenPipeError instead of BadStatusLine when the connection was reset
# ConnectionResetError happens on FreeBSD with Python 3.4
self.__conn.close()
self.__conn.request(method, path, postdata, headers)
return self._get_response()
示例7: request
# 需要导入模块: from http import client [as 别名]
# 或者: from http.client import BadStatusLine [as 别名]
def request(self, host, handler, request_body, verbose=0):
"""Send XMLRPC request"""
uri = '{scheme}://{host}{handler}'.format(scheme=self._scheme,
host=host, handler=handler)
if self._passmgr:
self._passmgr.add_password(None, uri, self._username,
self._password)
if self.verbose:
_LOGGER.debug("FabricTransport: {0}".format(uri))
opener = urllib2.build_opener(*self._handlers)
headers = {
'Content-Type': 'text/xml',
'User-Agent': self.user_agent,
}
req = urllib2.Request(uri, request_body, headers=headers)
try:
return self.parse_response(opener.open(req))
except (urllib2.URLError, urllib2.HTTPError) as exc:
try:
code = -1
if exc.code == 400:
reason = 'Permission denied'
code = exc.code
else:
reason = exc.reason
msg = "{reason} ({code})".format(reason=reason, code=code)
except AttributeError:
if 'SSL' in str(exc):
msg = "SSL error"
else:
msg = str(exc)
raise InterfaceError("Connection with Fabric failed: " + msg)
except BadStatusLine:
raise InterfaceError("Connection with Fabric failed: check SSL")
示例8: test_bad_status_repr
# 需要导入模块: from http import client [as 别名]
# 或者: from http.client import BadStatusLine [as 别名]
def test_bad_status_repr(self):
exc = client.BadStatusLine('')
self.assertEqual(repr(exc), '''BadStatusLine("\'\'",)''')
示例9: test_overflowing_status_line
# 需要导入模块: from http import client [as 别名]
# 或者: from http.client import BadStatusLine [as 别名]
def test_overflowing_status_line(self):
body = "HTTP/1.1 200 Ok" + "k" * 65536 + "\r\n"
resp = client.HTTPResponse(FakeSocket(body))
self.assertRaises((client.LineTooLong, client.BadStatusLine), resp.begin)
示例10: test_error_leak
# 需要导入模块: from http import client [as 别名]
# 或者: from http.client import BadStatusLine [as 别名]
def test_error_leak(self):
# Test that the socket is not leaked if getresponse() fails
conn = client.HTTPConnection('example.com')
response = None
class Response(client.HTTPResponse):
def __init__(self, *pos, **kw):
nonlocal response
response = self # Avoid garbage collector closing the socket
client.HTTPResponse.__init__(self, *pos, **kw)
conn.response_class = Response
conn.sock = FakeSocket('Invalid status line')
conn.request('GET', '/')
self.assertRaises(client.BadStatusLine, conn.getresponse)
self.assertTrue(response.closed)
self.assertTrue(conn.sock.file_closed)
示例11: sync_check
# 需要导入模块: from http import client [as 别名]
# 或者: from http.client import BadStatusLine [as 别名]
def sync_check(self):
url = '%s/synccheck' % self.loginInfo.get('syncUrl', self.loginInfo['url'])
params = {
'r' : int(time.time() * 1000),
'skey' : self.loginInfo['skey'],
'sid' : self.loginInfo['wxsid'],
'uin' : self.loginInfo['wxuin'],
'deviceid' : self.loginInfo['deviceid'],
'synckey' : self.loginInfo['synckey'],
'_' : self.loginInfo['logintime'], }
headers = { 'User-Agent' : config.USER_AGENT }
self.loginInfo['logintime'] += 1
try:
r = self.s.get(url, params=params, headers=headers, timeout=config.TIMEOUT)
except requests.exceptions.ConnectionError as e:
try:
if not isinstance(e.args[0].args[1], BadStatusLine):
raise
# will return a package with status '0 -'
# and value like:
# 6f:00:8a:9c:09:74:e4:d8:e0:14:bf:96:3a:56:a0:64:1b:a4:25:5d:12:f4:31:a5:30:f1:c6:48:5f:c3:75:6a:99:93
# seems like status of typing, but before I make further achievement code will remain like this
return '2'
except:
raise
r.raise_for_status()
regx = r'window.synccheck={retcode:"(\d+)",selector:"(\d+)"}'
pm = re.search(regx, r.text)
if pm is None or pm.group(1) != '0':
logger.debug('Unexpected sync check result: %s' % r.text)
return None
return pm.group(2)
示例12: __init__
# 需要导入模块: from http import client [as 别名]
# 或者: from http.client import BadStatusLine [as 别名]
def __init__(self):
httplib2.RETRIES = 1
self.MAX_RETRIES = 10
self.RETRIABLE_EXCEPTIONS = (httplib2.HttpLib2Error, IOError, httplib.NotConnected,
httplib.IncompleteRead, httplib.ImproperConnectionState,
httplib.CannotSendRequest, httplib.CannotSendHeader,
httplib.ResponseNotReady, httplib.BadStatusLine)
self.RETRIABLE_STATUS_CODES = [500, 502, 503, 504]
self.CLIENT_SECRETS_FILE = "client_secrets.json"
self.YOUTUBE_UPLOAD_SCOPE = "https://www.googleapis.com/auth/youtube.upload"
self.YOUTUBE_API_SERVICE_NAME = "youtube"
self.YOUTUBE_API_VERSION = "v3"
self.MISSING_CLIENT_SECRETS_MESSAGE = """
WARNING: Please configure OAuth 2.0
To make this sample run you will need to populate the client_secrets.json file
found at:
%s
with information from the Developers Console
https://console.developers.google.com/
For more information about the client_secrets.json file format, please visit:
https://developers.google.com/api-client-library/python/guide/aaa_client_secrets
""" % os.path.abspath(os.path.join(os.path.dirname(__file__),
self.CLIENT_SECRETS_FILE))
示例13: test_error_leak
# 需要导入模块: from http import client [as 别名]
# 或者: from http.client import BadStatusLine [as 别名]
def test_error_leak(self):
# Test that the socket is not leaked if getresponse() fails
conn = client.HTTPConnection('example.com')
response = None
class Response(client.HTTPResponse):
def __init__(self, *pos, **kw):
nonlocal response
response = self # Avoid garbage collector closing the socket
client.HTTPResponse.__init__(self, *pos, **kw)
conn.response_class = Response
conn.sock = FakeSocket('') # Emulate server dropping connection
conn.request('GET', '/')
self.assertRaises(client.BadStatusLine, conn.getresponse)
self.assertTrue(response.closed)
self.assertTrue(conn.sock.file_closed)
示例14: send_request
# 需要导入模块: from http import client [as 别名]
# 或者: from http.client import BadStatusLine [as 别名]
def send_request(self, req_inter):
try:
if self.logger:
self.logger.debug("SendRequest %s" % req_inter)
self.conn.request(req_inter.method, req_inter.uri, req_inter.data, req_inter.header)
self.conn.sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
try:
http_resp = self.conn.getresponse()
except BadStatusLine:
# open another connection when keep-alive timeout
# httplib will not handle keep-alive timeout, so we must handle it ourself
self.conn.close()
self.conn.request(req_inter.method, req_inter.uri, req_inter.data, req_inter.header)
self.conn.sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
http_resp = self.conn.getresponse()
headers = dict(http_resp.getheaders())
resp_inter = ResponseInternal(status=http_resp.status, header=headers, data=http_resp.read())
self.request_size = self.conn.request_length
self.response_size = len(resp_inter.data)
if not self.is_keep_alive():
self.conn.close()
if self.logger:
self.logger.debug("GetResponse %s" % resp_inter)
return resp_inter
except Exception as e:
self.conn.close()
raise MQClientNetworkException("NetWorkException", str(e)) # raise netException
示例15: __str__
# 需要导入模块: from http import client [as 别名]
# 或者: from http.client import BadStatusLine [as 别名]
def __str__(self):
"""Return error message for BadStatusLine Error."""
return '''BadStatusLine Exception when trying to reach {0}.
Are you using the correct webservice name?'''.format(self.url)