本文整理汇总了Python中http_parser.parser.HttpParser.get_version方法的典型用法代码示例。如果您正苦于以下问题:Python HttpParser.get_version方法的具体用法?Python HttpParser.get_version怎么用?Python HttpParser.get_version使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类http_parser.parser.HttpParser
的用法示例。
在下文中一共展示了HttpParser.get_version方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __call__
# 需要导入模块: from http_parser.parser import HttpParser [as 别名]
# 或者: from http_parser.parser.HttpParser import get_version [as 别名]
def __call__(self, addr):
'''Since an instance of HttpServer is passed to the Service
class (with appropriate request_handler established during
initialization), this __call__ method is what's actually
invoked by diesel.
'''
data = None
while True:
try:
h = HttpParser()
body = []
while True:
if data:
used = h.execute(data, len(data))
if h.is_headers_complete():
body.append(h.recv_body())
if h.is_message_complete():
data = data[used:]
break
data = receive()
env = h.get_wsgi_environ()
env.update({
'wsgi.version' : (1,0),
'wsgi.url_scheme' : 'http', # XXX incomplete
'wsgi.input' : cStringIO.StringIO(''.join(body)),
'wsgi.errors' : FileLikeErrorLogger(hlog),
'wsgi.multithread' : False,
'wsgi.multiprocess' : False,
'wsgi.run_once' : False,
})
req = Request(env)
resp = self.request_handler(req)
if 'Server' not in resp.headers:
resp.headers.add('Server', SERVER_TAG)
if 'Date' not in resp.headers:
resp.headers.add('Date', utcnow().strftime("%a, %d %b %Y %H:%M:%S UTC"))
assert resp, "HTTP request handler _must_ return a response"
self.send_response(resp, version=h.get_version())
if (not h.should_keep_alive()) or \
resp.headers.get('Connection', '').lower() == "close" or \
resp.headers.get('Content-Length') == None:
return
except ConnectionClosed:
break
示例2: HttpStream
# 需要导入模块: from http_parser.parser import HttpParser [as 别名]
# 或者: from http_parser.parser.HttpParser import get_version [as 别名]
class HttpStream(object):
""" An HTTP parser providing higher-level access to a readable,
sequential io.RawIOBase object. You can use implementions of
http_parser.reader (IterReader, StringReader, SocketReader) or
create your own.
"""
def __init__(self, stream, kind=HTTP_BOTH, decompress=False):
""" constructor of HttpStream.
:attr stream: an io.RawIOBase object
:attr kind: Int, could be 0 to parseonly requests,
1 to parse only responses or 2 if we want to let
the parser detect the type.
"""
self.parser = HttpParser(kind=kind, decompress=decompress)
self.stream = stream
def _check_headers_complete(self):
if self.parser.is_headers_complete():
return
while True:
try:
next(self)
except StopIteration:
if self.parser.is_headers_complete():
return
raise NoMoreData("Can't parse headers")
if self.parser.is_headers_complete():
return
def _wait_status_line(self, cond):
if self.parser.is_headers_complete():
return True
data = []
if not cond():
while True:
try:
d = next(self)
data.append(d)
except StopIteration:
if self.parser.is_headers_complete():
return True
raise BadStatusLine(b"".join(data))
if cond():
return True
return True
def _wait_on_url(self):
return self._wait_status_line(self.parser.get_url)
def _wait_on_status(self):
return self._wait_status_line(self.parser.get_status_code)
def url(self):
""" get full url of the request """
self._wait_on_url()
return self.parser.get_url()
def path(self):
""" get path of the request (url without query string and
fragment """
self._wait_on_url()
return self.parser.get_path()
def query_string(self):
""" get query string of the url """
self._wait_on_url()
return self.parser.get_query_string()
def fragment(self):
""" get fragment of the url """
self._wait_on_url()
return self.parser.get_fragment()
def version(self):
self._wait_on_status()
return self.parser.get_version()
def status_code(self):
""" get status code of a response as integer """
self._wait_on_status()
return self.parser.get_status_code()
def status(self):
""" return complete status with reason """
status_code = self.status_code()
reason = status_reasons.get(int(status_code), 'unknown')
return "%s %s" % (status_code, reason)
def method(self):
""" get HTTP method as string"""
self._wait_on_status()
return self.parser.get_method()
#.........这里部分代码省略.........
示例3: __call__
# 需要导入模块: from http_parser.parser import HttpParser [as 别名]
# 或者: from http_parser.parser.HttpParser import get_version [as 别名]
def __call__(self, addr):
'''Since an instance of HttpServer is passed to the Service
class (with appropriate request_handler established during
initialization), this __call__ method is what's actually
invoked by diesel.
'''
data = None
while True:
try:
h = HttpParser()
body = []
while True:
if data:
used = h.execute(data, len(data))
if h.is_headers_complete():
body.append(h.recv_body())
if h.is_message_complete():
data = data[used:]
break
data = receive()
env = h.get_wsgi_environ()
if 'HTTP_CONTENT_LENGTH' in env:
env['CONTENT_LENGTH'] = env.pop("HTTP_CONTENT_LENGTH")
if 'HTTP_CONTENT_TYPE' in env:
env['CONTENT_TYPE'] = env.pop("HTTP_CONTENT_TYPE")
env.update({
'wsgi.version' : (1,0),
'wsgi.url_scheme' : 'http', # XXX incomplete
'wsgi.input' : cStringIO.StringIO(''.join(body)),
'wsgi.errors' : FileLikeErrorLogger(hlog),
'wsgi.multithread' : False,
'wsgi.multiprocess' : False,
'wsgi.run_once' : False,
'REMOTE_ADDR' : addr[0],
'SERVER_NAME' : HOSTNAME,
'SERVER_PORT': str(self.port),
})
req = Request(env)
if req.headers.get('Connection', '').lower() == 'upgrade':
req.data = data
resp = self.request_handler(req)
if 'Server' not in resp.headers:
resp.headers.add('Server', SERVER_TAG)
if 'Date' not in resp.headers:
resp.headers.add('Date', utcnow().strftime("%a, %d %b %Y %H:%M:%S UTC"))
assert resp, "HTTP request handler _must_ return a response"
self.send_response(resp, version=h.get_version())
if (not h.should_keep_alive()) or \
resp.headers.get('Connection', '').lower() == "close" or \
resp.headers.get('Content-Length') == None:
return
# Switching Protocols
if resp.status_code == 101 and hasattr(resp, 'new_protocol'):
resp.new_protocol(req)
break
except ConnectionClosed:
break
示例4: HTTPProtocol
# 需要导入模块: from http_parser.parser import HttpParser [as 别名]
# 或者: from http_parser.parser.HttpParser import get_version [as 别名]
class HTTPProtocol(FlowControlMixin, asyncio.Protocol):
def __init__(self, stream_reader, callback, loop=None):
super().__init__(loop=loop)
self._stream_reader = stream_reader
self._stream_writer = None
self._callback = callback
self._task = None
self._server = None
def connection_made(self, transport):
self._parser = HttpParser()
self._stream_reader.set_transport(transport)
self._stream_writer = asyncio.StreamWriter(
transport,
self,
self._stream_reader,
self._loop,
)
# Grab the name of our socket if we have it
self._server = transport.get_extra_info("sockname")
def connection_lost(self, exc):
if exc is None:
self._stream_reader.feed_eof()
else:
self._stream_reader.set_exception(exc)
super().connection_lost(exc)
def data_received(self, data):
# Parse our incoming data with our HTTP parser
self._parser.execute(data, len(data))
# If we have not already handled the headers and we've gotten all of
# them, then invoke the callback with the headers in them.
if self._task is None and self._parser.is_headers_complete():
coro = self.dispatch(
{
"server": self._server,
"protocol": b"HTTP/" + b".".join(
str(x).encode("ascii")
for x in self._parser.get_version()
),
"method": self._parser.get_method().encode("latin1"),
"path": self._parser.get_path().encode("latin1"),
"query": self._parser.get_query_string().encode("latin1"),
"headers": self._parser.get_headers(),
},
self._stream_reader,
self._stream_writer,
)
self._task = asyncio.Task(coro, loop=self._loop)
# Determine if we have any data in the body buffer and if so feed it
# to our StreamReader
if self._parser.is_partial_body():
self._stream_reader.feed_data(self._parser.recv_body())
# Determine if we've completed the end of the HTTP request, if we have
# then we should close our stream reader because there is nothing more
# to read.
if self._parser.is_message_complete():
self._stream_reader.feed_eof()
def eof_received(self):
# We've gotten an EOF from the client, so we'll propagate this to our
# StreamReader
self._stream_reader.feed_eof()
@asyncio.coroutine
def dispatch(self, request, request_body, response):
# Get the status, headers, and body from the callback. The body must
# be iterable, and each item can either be a bytes object, or an
# asyncio coroutine, in which case we'll ``yield from`` on it to wait
# for it's value.
status, resp_headers, body = yield from self._callback(
request,
request_body,
)
# Write out the status line to the client for this request
# TODO: We probably don't want to hard code HTTP/1.1 here
response.write(b"HTTP/1.1 " + status + b"\r\n")
# Write out the headers, taking special care to ensure that any
# mandatory headers are added.
# TODO: We need to handle some required headers
for key, values in resp_headers.items():
# In order to handle headers which need to have multiple values
# like Set-Cookie, we allow the value of the header to be an
# iterable instead of a bytes object, in which case we'll write
# multiple header lines for this header.
if isinstance(values, (bytes, bytearray)):
values = [values]
#.........这里部分代码省略.........
示例5: __call__
# 需要导入模块: from http_parser.parser import HttpParser [as 别名]
# 或者: from http_parser.parser.HttpParser import get_version [as 别名]
def __call__(self, addr):
"""Since an instance of HttpServer is passed to the Service
class (with appropriate request_handler established during
initialization), this __call__ method is what's actually
invoked by diesel.
"""
data = None
while True:
try:
h = HttpParser()
body = []
while True:
if data:
used = h.execute(data, len(data))
if h.is_headers_complete():
body.append(h.recv_body())
if h.is_message_complete():
data = data[used:]
break
data = receive()
env = h.get_wsgi_environ()
if "HTTP_CONTENT_LENGTH" in env:
env["CONTENT_LENGTH"] = env.pop("HTTP_CONTENT_LENGTH")
if "HTTP_CONTENT_TYPE" in env:
env["CONTENT_TYPE"] = env.pop("HTTP_CONTENT_TYPE")
env.update(
{
"wsgi.version": (1, 0),
"wsgi.url_scheme": "http", # XXX incomplete
"wsgi.input": cStringIO.StringIO("".join(body)),
"wsgi.errors": FileLikeErrorLogger(hlog),
"wsgi.multithread": False,
"wsgi.multiprocess": False,
"wsgi.run_once": False,
"REMOTE_ADDR": addr[0],
"SERVER_NAME": HOSTNAME,
"SERVER_PORT": str(self.port),
}
)
req = Request(env)
if req.headers.get("Connection", "").lower() == "upgrade":
req.data = data
resp = self.request_handler(req)
if "Server" not in resp.headers:
resp.headers.add("Server", SERVER_TAG)
if "Date" not in resp.headers:
resp.headers.add("Date", utcnow().strftime("%a, %d %b %Y %H:%M:%S UTC"))
assert resp, "HTTP request handler _must_ return a response"
self.send_response(resp, version=h.get_version())
if (
(not h.should_keep_alive())
or resp.headers.get("Connection", "").lower() == "close"
or resp.headers.get("Content-Length") == None
):
return
except ConnectionClosed:
break
示例6: HttpProxyProtocol
# 需要导入模块: from http_parser.parser import HttpParser [as 别名]
# 或者: from http_parser.parser.HttpParser import get_version [as 别名]
class HttpProxyProtocol(asyncio.Protocol):
''' Implement HTTP(S) proxy behavior. '''
def __init__(self, loop, config, token_store):
''' Constructor. '''
self._parser = HttpParser()
self._body = b''
self._config = config
self._loop = loop
self._mitm = None
self._mitm_host = None
self._token_store = token_store
self._instagram = InstagramApi(
client_id=config['Instagram']['ClientID'],
client_secret=config['Instagram']['ClientSecret'],
)
self._twitter = TwitterApi(
consumer_key=config['Twitter']['ConsumerKey'],
consumer_secret=config['Twitter']['ConsumerSecret'],
app_token=config['Twitter']['AppToken'],
app_token_secret=config['Twitter']['AppTokenSecret'],
)
def connection_made(self, transport):
''' Save a reference to the transport so that we can send a reply. '''
log.debug('Connection opened.')
self._transport = transport
def connection_lost(self, exc):
log.debug('Connection closed.')
def data_received(self, data):
''' Parse incoming HTTP request. '''
log.debug('Data received: {}'.format(data))
self._parser.execute(data, len(data))
if self._parser.is_partial_body():
self._body += self._parser.recv_body()
if self._parser.is_message_complete():
method = self._parser.get_method()
uri = self._parser.get_url()
version = self._parser.get_version()
headers = self._parser.get_headers()
content_type = headers.get('Content-type', '')
charset = _get_charset(content_type)
body = self._body.decode(charset)
log.debug('Client charset: {}'.format(charset))
log.debug('Client status: method={} uri={} version={}' \
.format(method, uri, version))
log.debug('Client headers: {}'.format(headers))
log.debug('Client body: {}...'.format(body[:1000]))
if method == 'CONNECT':
asyncio.async(self._start_mitm(uri, version))
self._parser = HttpParser()
else:
asyncio.async(
self._request_upstream(
method,
uri,
version,
headers,
body
)
)
def start_tls(self, version):
'''
Initiate TLS session with the client.
This part is completely hacky! We mess around with the
transport's internals in order to wrap the current transport in TLS.
Python doesn't have an official way to do this, although it *might*
get fixed in 3.6: http://bugs.python.org/issue23749
'''
log.debug('The proxy is starting TLS with its client.')
status_line = 'HTTP/{}.{} {} {}\r\n\r\n' \
.format(version[0], version[1], 200, 'OK')
self._transport.write(status_line.encode('ascii'))
ssl_context = ssl.SSLContext(ssl.PROTOCOL_TLSv1)
ssl_context.set_ciphers('HIGH:!aNull:!eNull')
ssl_context.load_cert_chain('ssl/server.crt', 'ssl/server.key')
original_socket = self._transport._sock
self._transport = self._loop._make_ssl_transport(
original_socket,
self,
ssl_context,
server_side=True
)
#.........这里部分代码省略.........
示例7: MitmProtocol
# 需要导入模块: from http_parser.parser import HttpParser [as 别名]
# 或者: from http_parser.parser.HttpParser import get_version [as 别名]
class MitmProtocol(asyncio.Protocol):
''' Handles details of MITMing a TLS connection. '''
def __init__(self, loop, http_version, proxy):
''' Constructor. '''
self._http_version = http_version
self._loop = loop
self._parser = HttpParser()
self._proxy = proxy
self._received = asyncio.Future()
self._body = b''
def connection_made(self, transport):
''' Save a reference to the transport. '''
log.debug('MITM connection opened.')
self._transport = transport
cert = self._transport.get_extra_info('peercert')
log.debug('MITM upstream certificate: {}'.format(cert))
self._loop.call_soon(self._proxy.start_tls, self._http_version)
def connection_lost(self, exc):
log.debug('MITM connection closed.')
self._received.cancel()
def data_received(self, data):
''' Accumulate request data. '''
log.debug('MITM data received: {}'.format(data))
self._parser.execute(data, len(data))
if self._parser.is_partial_body():
self._body += self._parser.recv_body()
if self._parser.is_message_complete():
version = self._parser.get_version()
status = self._parser.get_status_code()
reason = None # For some reason, the parser doesn't expose this :(
headers = self._parser.get_headers()
log.debug('MITM upstream status: {}'.format(status))
log.debug('MITM upstream headers: {}'.format(headers))
log.debug('MITM upstream body: {}...'.format(self._body[:1000]))
self._received.set_result(
(version, status, reason, headers, self._body)
)
self._transport.close()
def forward(self, data):
''' Forward data to upstream host. '''
log.debug('MITM sending data: {}'.format(data))
self._transport.write(data)
@asyncio.coroutine
def receive(self):
''' Read data received by this MITM instance. '''
response = yield from self._received
return response