本文整理汇总了Python中http_parser.parser.HttpParser.get_headers方法的典型用法代码示例。如果您正苦于以下问题:Python HttpParser.get_headers方法的具体用法?Python HttpParser.get_headers怎么用?Python HttpParser.get_headers使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类http_parser.parser.HttpParser
的用法示例。
在下文中一共展示了HttpParser.get_headers方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: main
# 需要导入模块: from http_parser.parser import HttpParser [as 别名]
# 或者: from http_parser.parser.HttpParser import get_headers [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()
示例2: run
# 需要导入模块: from http_parser.parser import HttpParser [as 别名]
# 或者: from http_parser.parser.HttpParser import get_headers [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()
示例3: Response_Parser
# 需要导入模块: from http_parser.parser import HttpParser [as 别名]
# 或者: from http_parser.parser.HttpParser import get_headers [as 别名]
class Response_Parser():
def __init__(self):
self.parser = HttpParser()
self.len_response = 0
self.len_body = 0
self.body = None
def parse(self, raw_response):
self.len_response = len(bytearray(raw_response))
self.parser.execute(raw_response, self.len_response)
self.body = self.parser.recv_body()
self.len_body = len(bytearray(self.body))
def get_all_keys(self):
"""Get All the key in request headers."""
return self.parser.get_headers().keys()
def get_keys(self, *args):
header_keys = {}
for key in args:
header_keys[key] = self.parser.get_headers().get(key, '-')
return header_keys
def get_reponse(self, *args):
values = self.get_keys(*args)
status_code = self.parser.get_status_code()
obj = HTTP_Response(status_code, values, self.len_response, self.len_body)
return obj
def get_body(self):
return self.body
示例4: Request_Parser
# 需要导入模块: from http_parser.parser import HttpParser [as 别名]
# 或者: from http_parser.parser.HttpParser import get_headers [as 别名]
class Request_Parser():
def __init__(self):
self.parser = HttpParser()
self.len_request = 0
self.len_body = 0
def parse(self, raw_requset):
self.len_request = len(bytearray(raw_requset))
self.parser.execute(raw_requset, self.len_request)
self.len_body = len(bytearray(self.parser.recv_body()))
def get_all_keys(self):
"""Get All the key in request headers."""
return self.parser.get_headers().keys()
def get_keys(self, *args):
header_keys = {}
for key in args:
header_keys[key] = self.parser.get_headers().get(key, '-')
return header_keys
def get_request(self, *args):
values = self.get_keys(*args)
obj = HTTP_Requset(values, self.len_request, self.len_body)
return obj
def get_body(self):
return self.parser.recv_body()
示例5: handle_batch_client
# 需要导入模块: from http_parser.parser import HttpParser [as 别名]
# 或者: from http_parser.parser.HttpParser import get_headers [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()
示例6: iter_warc_records
# 需要导入模块: from http_parser.parser import HttpParser [as 别名]
# 或者: from http_parser.parser.HttpParser import get_headers [as 别名]
def iter_warc_records(warc_file, domain_whitelist=None, only_homepages=None):
""" Selective iterator over records in a WARC file """
for _, record in enumerate(warc_file):
if not record.url:
continue
if record['Content-Type'] != 'application/http; msgtype=response':
continue
url = URL(record.url, check_encoding=True)
if domain_whitelist is not None:
if url.domain not in domain_whitelist:
continue
elif only_homepages:
if url.parsed.path != "/" or url.parsed.query != "":
continue
payload = record.payload.read()
parser = HttpParser()
parser.execute(payload, len(payload))
headers = parser.get_headers()
if 'text/html' not in headers.get("content-type", ""):
# print "Not HTML?", record.url, headers
continue
yield url, headers, parser.recv_body()
示例7: make_request
# 需要导入模块: from http_parser.parser import HttpParser [as 别名]
# 或者: from http_parser.parser.HttpParser import get_headers [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
示例8: iter_items
# 需要导入模块: from http_parser.parser import HttpParser [as 别名]
# 或者: from http_parser.parser.HttpParser import get_headers [as 别名]
def iter_items(self, partition):
""" Yields objects in the source's native format """
warc_stream = self.open_warc_stream(partition["path"])
for record in warc_stream:
if not record.url:
continue
if record['Content-Type'] != 'application/http; msgtype=response':
continue
url = URL(record.url, check_encoding=True)
do_parse, index_level = self.qualify_url(url)
if not do_parse:
continue
payload = record.payload.read()
parser = HttpParser()
parser.execute(payload, len(payload))
headers = parser.get_headers()
if 'text/html' not in headers.get("content-type", ""):
# print "Not HTML?", record.url, headers
continue
yield url, headers, "html", index_level, parser.recv_body()
示例9: __init__
# 需要导入模块: from http_parser.parser import HttpParser [as 别名]
# 或者: from http_parser.parser.HttpParser import get_headers [as 别名]
def __init__(self, raw):
resp = HttpParser()
resp.execute(raw.response, len(raw.response))
self.headers = resp.get_headers()
self.body = "".join(resp._body)
self.raw = raw
self.code = resp.get_status_code()
self._json = None
示例10: get_appropriate_response
# 需要导入模块: from http_parser.parser import HttpParser [as 别名]
# 或者: from http_parser.parser.HttpParser import get_headers [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)
示例11: request
# 需要导入模块: from http_parser.parser import HttpParser [as 别名]
# 或者: from http_parser.parser.HttpParser import get_headers [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
示例12: main
# 需要导入模块: from http_parser.parser import HttpParser [as 别名]
# 或者: from http_parser.parser.HttpParser import get_headers [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()
示例13: proxy
# 需要导入模块: from http_parser.parser import HttpParser [as 别名]
# 或者: from http_parser.parser.HttpParser import get_headers [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' }
示例14: request
# 需要导入模块: from http_parser.parser import HttpParser [as 别名]
# 或者: from http_parser.parser.HttpParser import get_headers [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)
示例15: handleData
# 需要导入模块: from http_parser.parser import HttpParser [as 别名]
# 或者: from http_parser.parser.HttpParser import get_headers [as 别名]
def handleData(self,fd):
self.debug("Entering handleData")
if '\r\n\r\n' not in self.con_cache[fd]:
self.debug("Partial message - Exiting handleData")
return
p = HttpParser()
nparsed = p.execute(self.con_cache[fd],len(self.con_cache[fd]))
method = p.get_method()
path = p.get_path()
headers = p.get_headers()
debugStr = "\nMethod: %s\nPath: %s\nHeaders: %s\n" % (method,path,headers)
#self.debug(debugStr)
rangeRequest = None
if 'Range' in headers:
rangeRequest = headers['Range']
self.debug("Range: %s" % (rangeRequest))
validMethods = ['GET','HEAD','PUT','DELETE','POST']
isValid = False
if method not in validMethods:
response = self.makeError('400','Bad Request')
elif method != 'GET' and method != 'HEAD':
response = self.makeError('501','Not Implemented')
else:
if path == '/':
path = '/index.html'
path = self.hosts['default'] + path
(isValid,response) = self.makeResponse(path,rangeRequest)
self.clients[fd].send(response)
self.debug("PATH:%s"%(path))
if isValid and not rangeRequest and method != "HEAD":
self.sendAll(path,fd)
elif isValid and rangeRequest and method != "HEAD":
(start,end) = self.getByteRange(rangeRequest)
self.send(path,fd,start,end)
self.debug("Exiting handleData")