本文整理汇总了Python中http_parser.parser.HttpParser.is_headers_complete方法的典型用法代码示例。如果您正苦于以下问题:Python HttpParser.is_headers_complete方法的具体用法?Python HttpParser.is_headers_complete怎么用?Python HttpParser.is_headers_complete使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类http_parser.parser.HttpParser
的用法示例。
在下文中一共展示了HttpParser.is_headers_complete方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: make_request
# 需要导入模块: from http_parser.parser import HttpParser [as 别名]
# 或者: from http_parser.parser.HttpParser import is_headers_complete [as 别名]
def make_request(sock, server_name):
"""
Given an open socket, makes a simple HTTP request, parses the response, and
returns a dictionary containing the HTTP headers that were returned by the
server.
"""
p = HttpParser()
request = ('GET / HTTP/1.0\r\n' +
'User-Agent: pySSLScan\r\n' +
'Host: %s\r\n\r\n' % (server_name,))
sock.write(request.encode('ascii'))
headers = None
while True:
data = sock.recv(1024)
if not data:
break
recved = len(data)
nparsed = p.execute(data, recved)
assert nparsed == recved
if p.is_headers_complete():
headers = p.get_headers()
break
return headers
示例2: main
# 需要导入模块: from http_parser.parser import HttpParser [as 别名]
# 或者: from http_parser.parser.HttpParser import is_headers_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()
示例3: handle_batch_client
# 需要导入模块: from http_parser.parser import HttpParser [as 别名]
# 或者: from http_parser.parser.HttpParser import is_headers_complete [as 别名]
def handle_batch_client(sock):
recvbuf = ""
while True:
rds, _, _ = select.select([sock], [], [], 60 * 5)
if not rds:
break
data = sock.recv(1024)
if not data:
break
recvbuf += data
pos = recvbuf.find("\r\n\r\n")
if pos == -1:
continue
parser = HttpParser()
nparsed = parser.execute(recvbuf, pos + 4)
if nparsed != pos + 4:
logging.debug("pos:%d, nparsed:%d, recvbuf:%r", pos, nparsed, recvbuf)
assert nparsed == pos + 4
assert parser.is_headers_complete()
headers = parser.get_headers()
content_length = int(headers["Content-Length"]) if headers.has_key("Content-Length") else 0
logging.debug("content length:%d", content_length)
recvbuf = recvbuf[pos + 4 :]
preread = recvbuf[:content_length]
recvbuf = recvbuf[content_length:]
keepalived = handle_request(sock, parser, preread)
if not keepalived:
break
logging.debug("close client")
sock.close()
示例4: run
# 需要导入模块: from http_parser.parser import HttpParser [as 别名]
# 或者: from http_parser.parser.HttpParser import is_headers_complete [as 别名]
def run(self):
HTTP_Request = self.client.recv(self.max)
p = HttpParser()
header_done = False
destination_host = ''
if HTTP_Request:
print 'Got something from ' + str(self.address) + '...'
request_length = len(HTTP_Request)
nparsed = p.execute(HTTP_Request, request_length)
assert nparsed == request_length
if p.is_headers_complete() and not header_done:
print(p.get_headers())
print(p.get_headers()['Host'])
destination_host = p.get_headers()['Host']
header_done = True
Relay_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
Relay_socket.connect((destination_host,80))
Relay_socket.sendall(HTTP_Request)
print 'Forwarding data to destination host...'
while True:
HTTP_Response = Relay_socket.recv(self.max)
if not HTTP_Response:
break
else:
print 'Received data back. Forwarding to the client...'
self.client.sendall(HTTP_Response)
self.client.close()
Relay_socket.close()
示例5: request
# 需要导入模块: from http_parser.parser import HttpParser [as 别名]
# 或者: from http_parser.parser.HttpParser import is_headers_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
示例6: proxy
# 需要导入模块: from http_parser.parser import HttpParser [as 别名]
# 或者: from http_parser.parser.HttpParser import is_headers_complete [as 别名]
def proxy(data):
"""
the function called by tproxy to determine where to send traffic
tproxy will call this function repeatedly for the same connection, as we
receive more incoming data, until we return something other than None.
typically our response tells tproxy where to proxy the connection to, but
may also tell it to hang up, or respond with some error message.
"""
log = logging.getLogger("proxy")
bytes_received = len(data)
parser = HttpParser()
bytes_parsed = parser.execute(data, bytes_received)
if bytes_parsed != bytes_received:
return { 'close':
'HTTP/1.0 400 Bad Request\r\n\r\nParse error' }
if not parser.is_headers_complete():
if bytes_received > MAX_HEADER_LENGTH:
return { 'close':
'HTTP/1.0 400 Bad Request\r\n'
'\r\nHeaders are too large' }
return None
headers = parser.get_headers()
# the hostname may be in the form of hostname:port, in which case we want
# to discard the port, and route just on hostname
route_host = headers.get('HOST', None)
if route_host:
match = _HOST_PORT_REGEXP.match(route_host)
if match:
route_host = match.group(1)
try:
log.debug("Routing %r" % ( parser.get_url(), ))
return _ROUTER.route(
route_host,
parser.get_method(),
parser.get_path(),
parser.get_query_string())
except Exception, err:
log.error("error routing %r, %s" % (
parser.get_url(), traceback.format_exc(), ))
gevent.sleep(ERROR_DELAY)
return { 'close':
'HTTP/1.0 502 Gateway Error\r\n'
'\r\nError routing request' }
示例7: proxy
# 需要导入模块: from http_parser.parser import HttpParser [as 别名]
# 或者: from http_parser.parser.HttpParser import is_headers_complete [as 别名]
def proxy(self, data):
# parse headers
recved = len(data)
parser = HttpParser()
nparsed = parser.execute(data, recved)
if nparsed != recved:
return {"close": True}
if not parser.is_headers_complete():
return
# get remote
return self.lookup(parser)
示例8: __call__
# 需要导入模块: from http_parser.parser import HttpParser [as 别名]
# 或者: from http_parser.parser.HttpParser import is_headers_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_headers_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')
示例10: makeRequest
# 需要导入模块: from http_parser.parser import HttpParser [as 别名]
# 或者: from http_parser.parser.HttpParser import is_headers_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)
示例11: receive
# 需要导入模块: from http_parser.parser import HttpParser [as 别名]
# 或者: from http_parser.parser.HttpParser import is_headers_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(),
)
示例12: get_appropriate_response
# 需要导入模块: from http_parser.parser import HttpParser [as 别名]
# 或者: from http_parser.parser.HttpParser import is_headers_complete [as 别名]
def get_appropriate_response(self):
try:
# try to use the fast C parser
from http_parser.parser import HttpParser
except ImportError:
# fall back to the Python parser
from http_parser.pyparser import HttpParser
p = HttpParser()
nparsed = p.execute(self.content.encode('utf-8'), len(self.content))
if not p.is_headers_complete():
return HttpResponseBadRequest(content_f=BAD_REQUEST_HTML)
# check method
if p.get_method() not in SUPPORTED_METHODS:
return HttpResponseNotImplemented(content_f=NOT_IMPLEMENTED_HTML)
base_filepath = ''
try:
base_filepath = settings.HOSTS[p.get_headers()['Host'].split(':')[0]]
except KeyError:
base_filepath = settings.HOSTS['default']
req_file = self.content.split(' ')[1]
if req_file == '/':
req_file = '/index.html'
try:
full_path = base_filepath + req_file
open(full_path)
if p.get_method() == 'HEAD':
return HttpResponse(content_f=full_path, method='HEAD')
if 'Range' in p.get_headers():
return HttpResponsePartialContent(content_f=full_path, h_range=p.get_headers()['Range'])
return HttpResponse(content_f=full_path)
except IOError as err:
if err.errno == 13:
return HttpResponseForbidden(content_f=FORBIDDEN_HTML)
elif err.errno == 2:
return HttpResponseNotFound(content_f=NOT_FOUND_HTML)
return HttpResponseServerError(content_f=SERVER_ERROR_HTML)
示例13: main
# 需要导入模块: from http_parser.parser import HttpParser [as 别名]
# 或者: from http_parser.parser.HttpParser import is_headers_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()
示例14: CometaClient
# 需要导入模块: from http_parser.parser import HttpParser [as 别名]
# 或者: from http_parser.parser.HttpParser import is_headers_complete [as 别名]
class CometaClient(object):
"""Connect a device to the Cometa infrastructure"""
errors = {0:'ok', 1:'timeout', 2:'network error', 3:'protocol error', 4:'authorization error', 5:'wrong parameters', 9:'internal error'}
def __init__(self,server, port, application_id):
"""
The Cometa instance constructor.
server: the Cometa server FQDN
port: the Cometa server port
application_id: the Cometa application ID
"""
self.error = 9
self.debug = False
self._server = server
self._port = port
self._app_id = application_id
self._message_cb = None
self._device_id = ""
self._platform = ""
self._hparser = None
self._sock = None #socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self._heartbeat_rate = 60
self._trecv = None
self._thbeat = None
self._hb_lock = threading.Lock()
self._reconnecting = False
return
def attach(self, device_id, device_info):
"""
Attach the specified device to a Cometa registered application.
Authentication is done using only the application_id (one-way authentication).
device_id: the device unique identifier
device_info: a description of the platform or the device (used only as a comment)
"""
self._device_id = device_id
self._platform = device_info
self._hparser = HttpParser()
self._sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
self._sock.connect((self._server, self._port))
sendBuf="POST /v1/applications/%s/devices/%s HTTP/1.1\r\nHost: api.cometa.io\r\nContent-Length:%d\r\n\r\n%s" % (self._app_id,device_id,len(device_info),device_info)
self._sock.send(sendBuf)
recvBuf = ""
while True:
data = self._sock.recv(1024)
if not data:
break
dataLen = len(data)
nparsed = self._hparser.execute(data, dataLen)
assert nparsed == dataLen
if self._hparser.is_headers_complete():
if self.debug:
print "connection for device %s complete" % (device_id)
print self._hparser.get_headers()
# reading the attach complete message from the server
# i.e. {"msg":"200 OK","heartbeat":60,"timestamp":1441382935}
recvBuf = self._hparser.recv_body()
#TODO: check for error in connecting, i.e. 403 already connected
if len(recvBuf) < 16 or recvBuf[1:15] != '"msg":"200 OK"':
self.error = 5
return recvBuf
# reset error
self.error = 0
# set the socket non blocking
self._sock.setblocking(0)
# do not (re)start the threads during a reconnection
if self._reconnecting:
self._reconnecting = False
return recvBuf
# start the hearbeat thread
self._thbeat = threading.Thread(target=self._heartbeat)
self._thbeat.daemon = True
self._thbeat.start()
# start the receive thread
self._trecv = threading.Thread(target=self._receive)
self._trecv.daemon = True # force to exit on SIGINT
self._trecv.start()
return recvBuf
except:
self.error = 2
return
def send_data(self, msg):
"""
Send a data event message upstream to the Cometa server.
If a Webhook is specified for the Application in the Cometa configuration file /etc/cometa.conf on the server,
the message is relayed to the Webhook. Also, the Cometa server propagates the message to all open devices Websockets.
#.........这里部分代码省略.........
示例15: __call__
# 需要导入模块: from http_parser.parser import HttpParser [as 别名]
# 或者: from http_parser.parser.HttpParser import is_headers_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()
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