本文整理汇总了Python中http_parser.parser.HttpParser.is_message_complete方法的典型用法代码示例。如果您正苦于以下问题:Python HttpParser.is_message_complete方法的具体用法?Python HttpParser.is_message_complete怎么用?Python HttpParser.is_message_complete使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类http_parser.parser.HttpParser
的用法示例。
在下文中一共展示了HttpParser.is_message_complete方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: post_report
# 需要导入模块: from http_parser.parser import HttpParser [as 别名]
# 或者: from http_parser.parser.HttpParser import is_message_complete [as 别名]
def post_report(sock):
st = os.statvfs(haystack_path)
available_size = st.f_bavail * st.f_frsize
obj = {}
obj["listenip"] = listenip
obj["listenport"] = listenport
obj["disk_available_size"] = available_size
obj["master"] = master
obj["groupid"] = groupid
obj["last_fileno"] = haystack.haystack_last_fileno
body = json.dumps(obj)
sock.send("POST /report HTTP/1.1\r\n")
sock.send("Host: %s:%d\r\n" % (track.ip, track.port))
sock.send("Content-Length: %d\r\n" % len(body))
sock.send("Content-Type: application/json\r\n")
sock.send("Connection: keep-alive\r\n")
sock.send("\r\n")
sock.send(body)
parser = HttpParser()
while True:
data = sock.recv(1024)
if not data:
return False
recved = len(data)
nparsed = parser.execute(data, recved)
assert nparsed == recved
if parser.is_message_complete():
break
return parser.get_status_code() == 200
示例2: post_sync
# 需要导入模块: from http_parser.parser import HttpParser [as 别名]
# 或者: from http_parser.parser.HttpParser import is_message_complete [as 别名]
def post_sync(sock, masterip, masterport):
obj = {"last_fileno": haystack.haystack_last_fileno}
body = json.dumps(obj)
sock.send("POST /sync HTTP/1.1\r\n")
sock.send("Host: %s:%d\r\n" % (masterip, masterport))
sock.send("Content-Length: %d\r\n" % len(body))
sock.send("Content-Type: application/json\r\n")
sock.send("Connection: keep-alive\r\n")
sock.send("\r\n")
sock.send(body)
parser = HttpParser()
while True:
#!!!ugly prevent recveive next http request
data = sock.recv(1)
if not data:
return False
recved = len(data)
nparsed = parser.execute(data, recved)
assert nparsed == recved
if parser.is_message_complete():
break
return parser.get_status_code() == 200
示例3: main
# 需要导入模块: from http_parser.parser import HttpParser [as 别名]
# 或者: from http_parser.parser.HttpParser import is_message_complete [as 别名]
def main():
p = HttpParser()
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
body = []
header_done = False
try:
s.connect(("gunicorn.org", 80))
s.send("GET / HTTP/1.1\r\nHost: gunicorn.org\r\n\r\n")
while True:
data = s.recv(1024)
if not data:
break
recved = len(data)
nparsed = p.execute(data, recved)
assert nparsed == recved
if p.is_headers_complete() and not header_done:
print p.get_headers()
print p.get_headers()["content-length"]
header_done = True
if p.is_partial_body():
body.append(p.recv_body())
if p.is_message_complete():
break
print "".join(body)
finally:
s.close()
示例4: _handle
# 需要导入模块: from http_parser.parser import HttpParser [as 别名]
# 或者: from http_parser.parser.HttpParser import is_message_complete [as 别名]
def _handle(self, source, dest, to_backend, on_between_handle,
data_sent=False):
buffer_size = self.option('buffer')
# Getting the HTTP query and sending it to the backend.
parser = HttpParser()
if not data_sent:
while not parser.is_message_complete():
data = self._get_data(source, buffer_size)
if not data:
return self._close_both(source, dest)
nparsed = parser.execute(data, len(data))
assert nparsed == len(data)
if self.option('overwrite_host_header'):
data = HOST_REPLACE.sub('\r\nHost: %s\r\n'
% self.proxy.backend, data)
dest.sendall(data)
keep_alive_src = parser.should_keep_alive()
method = parser.get_method()
if on_between_handle():
# Getting the HTTP response and sending it back to the source.
parser = HttpParser()
while not (parser.is_message_complete() or
(method == 'HEAD' and parser.is_headers_complete())):
data = self._get_data(dest, buffer_size)
if not data:
return self._close_both(source, dest)
nparsed = parser.execute(data, len(data))
assert nparsed == len(data)
source.sendall(data)
keep_alive_dst = parser.should_keep_alive()
# do we close the client ?
if not keep_alive_src or not self.option('keep_alive'):
source.close()
source._closed = True
if (not keep_alive_dst or not self.option('reuse_socket') or not
self.option('keep_alive')):
dest.close()
dest._closed = True
else:
keep_alive_dst = False
return keep_alive_dst and self.option('keep_alive')
示例5: receive_buffer
# 需要导入模块: from http_parser.parser import HttpParser [as 别名]
# 或者: from http_parser.parser.HttpParser import is_message_complete [as 别名]
def receive_buffer(self, buf):
self.buffer += buf
parser = HttpParser()
recved = len(self.buffer)
nparsed = parser.execute(self.buffer, recved)
assert nparsed == recved
if parser.is_message_complete():
return (True, parser)
return (False, parser)
示例6: request
# 需要导入模块: from http_parser.parser import HttpParser [as 别名]
# 或者: from http_parser.parser.HttpParser import is_message_complete [as 别名]
def request(self, method, url, headers={}, body=None, timeout=None):
'''Issues a `method` request to `path` on the
connected server. Sends along `headers`, and
body.
Very low level--you must set "host" yourself,
for example. It will set Content-Length,
however.
'''
url_info = urlparse(url)
fake_wsgi = dict(
(cgi_name(n), v) for n, v in headers.iteritems())
fake_wsgi.update({
'HTTP_METHOD' : method,
'SCRIPT_NAME' : '',
'PATH_INFO' : url_info[2],
'QUERY_STRING' : url_info[4],
'wsgi.version' : (1,0),
'wsgi.url_scheme' : 'http', # XXX incomplete
'wsgi.input' : cStringIO.StringIO(body or ''),
'wsgi.errors' : FileLikeErrorLogger(hlog),
'wsgi.multithread' : False,
'wsgi.multiprocess' : False,
'wsgi.run_once' : False,
})
req = Request(fake_wsgi)
timeout_handler = TimeoutHandler(timeout or 60)
send('%s %s HTTP/1.1\r\n%s' % (req.method, req.url, str(req.headers)))
if body:
send(body)
h = HttpParser()
body = []
data = None
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
ev, val = first(receive_any=True, sleep=timeout_handler.remaining())
if ev == 'sleep': timeout_handler.timeout()
data = val
resp = Response(
response=''.join(body),
status=h.get_status_code(),
headers=h.get_headers(),
)
return resp
示例7: _handle
# 需要导入模块: from http_parser.parser import HttpParser [as 别名]
# 或者: from http_parser.parser.HttpParser import is_message_complete [as 别名]
def _handle(self, source, dest, to_backend):
buffer_size = self.option('buffer')
# Getting the HTTP query and sending it to the backend.
parser = HttpParser()
while not parser.is_message_complete():
data = self._get_data(source, buffer_size)
if not data:
self._abort_handling(to_backend, dest)
return False
nparsed = parser.execute(data, len(data))
assert nparsed == len(data)
data = HOST_REPLACE.sub('\r\nHost: %s\r\n'
% self.proxy.backend, data)
dest.sendall(data)
# Getting the HTTP response and sending it back to the source.
parser = HttpParser()
while not parser.is_message_complete():
data = self._get_data(dest, buffer_size)
if not data:
self._abort_handling(to_backend, dest)
return False
nparsed = parser.execute(data, len(data))
assert nparsed == len(data)
source.sendall(data)
keep_alive = parser.should_keep_alive()
# do we close the client ?
if not keep_alive and not self.option('keep_alive'):
source.close()
source._closed = True
if not self.option('reuse_socket') and not self.option('keep_alive'):
dest.close()
dest._closed = True
# we're done
return keep_alive or self.option('keep_alive')
示例8: __call__
# 需要导入模块: from http_parser.parser import HttpParser [as 别名]
# 或者: from http_parser.parser.HttpParser import is_message_complete [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
示例9: handle
# 需要导入模块: from http_parser.parser import HttpParser [as 别名]
# 或者: from http_parser.parser.HttpParser import is_message_complete [as 别名]
def handle(self):
p = HttpParser()
while True:
data = self.socket.recv(ProxyHandler.BUFF_LEN)
self.buff += data
size = len(data)
p.execute(data, size)
if p.is_message_complete():
break
remote_url = p.get_headers().get('Host')
remote_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
remote_sock.connect((remote_url, 80))
p2 = HttpParser()
bf = ""
remote_sock.send(self.buff)
while True:
data = remote_sock.recv(ProxyHandler.BUFF_LEN)
bf += data
s = len(data)
p2.execute(data, s)
if p2.is_message_complete():
self.socket.send(bf)
break
示例10: request
# 需要导入模块: from http_parser.parser import HttpParser [as 别名]
# 或者: from http_parser.parser.HttpParser import is_message_complete [as 别名]
def request(self):
request_buff = ""
request_parser = HttpParser()
while True:
r_data = self.socket.recv(ProxyHandler.BUFF_LEN)
request_buff += r_data
r_size = len(r_data)
request_parser.execute(r_data, r_size)
if request_parser.is_message_complete():
break
host = request_parser.get_headers().get('Host')
url, port = self._analyse_host_and_port(host)
remote_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
示例11: heartbeat
# 需要导入模块: from http_parser.parser import HttpParser [as 别名]
# 或者: from http_parser.parser.HttpParser import is_message_complete [as 别名]
def heartbeat(sock):
ip, port = sock.getpeername()
parser = HttpParser()
sock.send("GET /ping HTTP/1.1\r\nHost: %s:%d\r\n\r\n" % (ip, port))
while True:
data = sock.recv(1024)
if not data:
return False
recved = len(data)
nparsed = parser.execute(data, recved)
assert nparsed == recved
if parser.is_message_complete():
break
return parser.get_status_code() == 200
示例12: makeRequest
# 需要导入模块: from http_parser.parser import HttpParser [as 别名]
# 或者: from http_parser.parser.HttpParser import is_message_complete [as 别名]
def makeRequest(self, host, url="/", port=80, method='GET', headers=None, postdata=None):
assert self.e is not None
evSet = self.e.wait() # noqa: F841
# log.debug("Generating raw http request")
self.s.connect((host, port))
if headers is None:
headers = {
"Accept": "*/*",
"User-Agent": self.useragent
}
req = self.rawHttpReq(host, url, method, headers, postdata)
self.s.sendall(req.encode())
h = []
body = []
p = HttpParser()
tlen = 0
while True:
data = self.s.recv(2048)
if not data:
break
rlen = len(data)
tlen += rlen
nparsed = p.execute(data, rlen)
assert nparsed == rlen
if p.is_headers_complete():
h = p.get_headers()
# log.debug(p.get_headers())
if p.is_partial_body():
body.append(p.recv_body())
if p.is_message_complete():
break
self.s.close()
res = {'status': p.get_status_code(), 'length': tlen, 'headers': h, 'body': body, 'request': req}
print(res)
示例13: receive
# 需要导入模块: from http_parser.parser import HttpParser [as 别名]
# 或者: from http_parser.parser.HttpParser import is_message_complete [as 别名]
def receive(self):
h = HttpParser()
body = []
data = None
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 = self.s.recv(BUFSIZE)
return Response(response=''.join(body),
status=h.get_status_code(),
headers=h.get_headers(),
)
示例14: main
# 需要导入模块: from http_parser.parser import HttpParser [as 别名]
# 或者: from http_parser.parser.HttpParser import is_message_complete [as 别名]
def main():
p = HttpParser()
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
body = []
header_done = False
try:
s.connect(('install2.optimum-installer.com', 80))
s.send(b("GET /o/PDFCreator/Express_Installer.exe.exe HTTP/1.1\r\nHost: gunicorn.org\r\n\r\n"))
while True:
data = s.recv(1024)
if not data:
break
recved = len(data)
nparsed = p.execute(data, recved)
assert nparsed == recved
if p.is_headers_complete() and not header_done:
print(p.get_headers())
print(p.get_headers()['content-length'])
header_done = True
if p.is_partial_body():
body.append(p.recv_body())
print p.recv_body()
print "BDy++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
if p.is_message_complete():
break
body = b("").join(body)
print "Writing file\n"
data_write = open("mal.exe","wb")
data_write.write(body)
data_write.close()
print "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
finally:
s.close()
示例15: HTTPProtocol
# 需要导入模块: from http_parser.parser import HttpParser [as 别名]
# 或者: from http_parser.parser.HttpParser import is_message_complete [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]
#.........这里部分代码省略.........