我们从Python开源项目中,提取了以下10个代码示例,用于说明如何使用six.moves.http_client.HTTPResponse()。
def HTMLInputStream(source, encoding=None, parseMeta=True, chardet=True): if isinstance(source, http_client.HTTPResponse): # Work around Python bug #20007: read(0) closes the connection. # http://bugs.python.org/issue20007 isUnicode = False elif hasattr(source, "read"): isUnicode = isinstance(source.read(0), text_type) else: isUnicode = isinstance(source, text_type) if isUnicode: if encoding is not None: raise TypeError("Cannot explicitly set an encoding with a unicode string") return HTMLUnicodeInputStream(source) else: return HTMLBinaryInputStream(source, encoding, parseMeta, chardet)
def close(self): ''' Close the connection to server. If available, return a http_client.HTTPResponse object. Closing the connection involves sending the Transfer-Encoding terminating bytes. ''' self._reset_retries() self._closed = True # Chunked-encoded posts are terminated with '0\r\n\r\n' # For some reason, either Python or node.js seems to # require an extra \r\n. try: self._conn.send('\r\n0\r\n\r\n'.encode('utf-8')) except http_client.socket.error: # In case the socket has already been closed return '' return self._getresponse()
def HTMLInputStream(source, **kwargs): # Work around Python bug #20007: read(0) closes the connection. # http://bugs.python.org/issue20007 if (isinstance(source, http_client.HTTPResponse) or # Also check for addinfourl wrapping HTTPResponse (isinstance(source, urllib.response.addbase) and isinstance(source.fp, http_client.HTTPResponse))): isUnicode = False elif hasattr(source, "read"): isUnicode = isinstance(source.read(0), text_type) else: isUnicode = isinstance(source, text_type) if isUnicode: encodings = [x for x in kwargs if x.endswith("_encoding")] if encodings: raise TypeError("Cannot set an encoding with a unicode input, set %r" % encodings) return HTMLUnicodeInputStream(source, **kwargs) else: return HTMLBinaryInputStream(source, **kwargs)
def connect(self): TimeoutHTTPProxyConnection.connect(self) host = "%s:%s" % (self.real_host, self.real_port) TimeoutHTTPConnection.putrequest(self, "CONNECT", host) self._add_auth_proxy_header() TimeoutHTTPConnection.endheaders(self) class MyHTTPSResponse(httplib.HTTPResponse): def begin(self): httplib.HTTPResponse.begin(self) self.will_close = 0 response_class = self.response_class self.response_class = MyHTTPSResponse response = httplib.HTTPConnection.getresponse(self) self.response_class = response_class response.close() if response.status != 200: self.close() raise socket.error(1001, response.status, response.msg) self.sock = ssl.wrap_socket(self.sock, keyfile=self.key_file, certfile=self.cert_file)
def request(self, method, path, body="", headers=None): """Requests made via this connection actually get translated and routed into our WSGI app, we then wait for the response and turn it back into an `httplib.HTTPResponse`. """ if not headers: headers = {} req = webob.Request.blank(path) req.method = method req.headers = headers req.host = self.host req.body = encodeutils.safe_encode(body) resp = str(req.get_response(self.app)) resp = "HTTP/1.0 %s" % resp sock = FakeHttplibSocket(resp) self.http_response = httplib.HTTPResponse(sock) self.http_response.begin()
def _getresponse(self): ''' Read from recv and return a HTTPResponse object if possible. Either 1 - The client has succesfully closed the connection: Return '' 2 - The server has already closed the connection: Return the response if possible. ''' # Wait for a response self._conn.sock.setblocking(True) # Parse the response response = self._bytes while True: try: _bytes = self._conn.sock.recv(1) except http_client.socket.error: # For error 54: Connection reset by peer # (and perhaps others) return six.b('') if _bytes == six.b(''): break else: response += _bytes # Set recv to be non-blocking again self._conn.sock.setblocking(False) # Convert the response string to a http_client.HTTPResponse # object with a bit of a hack if response != six.b(''): # Taken from # http://pythonwise.blogspot.ca/2010/02/parse-http-response.html try: response = http_client.HTTPResponse(_FakeSocket(response)) response.begin() except: # Bad headers ... etc. response = six.b('') return response
def begin(self): """Our connection will never be automatically closed, so set will_close to False.""" http_client.HTTPResponse.begin(self) self.will_close = False return
def write(self, data, reconnect_on=('', 200, )): ''' Send `data` to the server in chunk-encoded form. Check the connection before writing and reconnect if disconnected and if the response status code is in `reconnect_on`. The response may either be an HTTPResponse object or an empty string. ''' if not self._isconnected(): # Attempt to get the response. response = self._getresponse() # Reconnect depending on the status code. if ((response == '' and '' in reconnect_on) or (response and isinstance(response, http_client.HTTPResponse) and response.status in reconnect_on)): self._reconnect() elif response and isinstance(response, http_client.HTTPResponse): # If an HTTPResponse was recieved then # make the users aware instead of # auto-reconnecting in case the # server is responding with an important # message that might prevent # future requests from going through, # like Invalid Credentials. # This allows the user to determine when # to reconnect. raise Exception("Server responded with " "status code: {status_code}\n" "and message: {msg}." .format(status_code=response.status, msg=response.read())) elif response == '': raise Exception("Attempted to write but socket " "was not connected.") try: msg = data msglen = format(len(msg), 'x') # msg length in hex # Send the message in chunk-encoded form self._conn.sock.setblocking(1) self._conn.send('{msglen}\r\n{msg}\r\n' .format(msglen=msglen, msg=msg).encode('utf-8')) self._conn.sock.setblocking(0) except http_client.socket.error: self._reconnect() self.write(data)