我需要访问浏览器在web.py
发送给服务器的原始http请求。
,例如,当我浏览某个页面时,这是Chromium发出的请求:
$ nc -l 8081
GET / HTTP/1.1
Host: 127.0.0.1:8081
Connection: keep-alive
Cache-Control: max-age=0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
User-Agent: Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.22 (KHTML, like Gecko) Ubuntu Chromium/25.0.1364.160 Chrome/25.0.1364.160 Safari/537.22
Accept-Encoding: gzip,deflate,sdch
Accept-Language: it-IT,it;q=0.8,en-US;q=0.6,en;q=0.4
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3
我试图从web.ctx.env
获取,但这是一本字典(虽然我更喜欢原始的原始文本请求)并且它与其他一些数据混合在一起:
SERVER_SOFTWARE: CherryPy/3.2.0 Server
SCRIPT_NAME:
ACTUAL_SERVER_PROTOCOL: HTTP/1.1
REQUEST_METHOD: GET
PATH_INFO: /
SERVER_PROTOCOL: HTTP/1.1
QUERY_STRING:
HTTP_ACCEPT_CHARSET: ISO-8859-1,utf-8;q=0.7,*;q=0.3
HTTP_USER_AGENT: Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.22 (KHTML, like Gecko) Ubuntu Chromium/25.0.1364.160 Chrome/25.0.1364.160 Safari/537.22
HTTP_CONNECTION: keep-alive
REMOTE_PORT: 55409
SERVER_NAME: localhost
REMOTE_ADDR: 127.0.0.1
wsgi.url_scheme: http
SERVER_PORT: 8081
wsgi.input: <web.wsgiserver.KnownLengthRFile object at 0x940b16c>
HTTP_HOST: 127.0.0.1:8081
wsgi.multithread: True
REQUEST_URI: /
HTTP_ACCEPT: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
wsgi.version: (1, 0)
wsgi.run_once: False
wsgi.errors: <open file '<stderr>', mode 'w' at 0xb73010d0>
wsgi.multiprocess: False
HTTP_ACCEPT_LANGUAGE: it-IT,it;q=0.8,en-US;q=0.6,en;q=0.4
HTTP_ACCEPT_ENCODING: gzip,deflate,sdch
这是我用来获取上面输出的代码:
#!/usr/bin/env python
import web
urls = ('(.*)', 'urlhandler')
class urlhandler:
def GET(self, url):
txt = ""
for k, v in web.ctx.env.items():
txt += ": ".join([k, str(v)]) + "\n"
return txt
if __name__ == '__main__':
app = web.application(urls, globals())
app.run()
我是否应该从不需要的数据中清除此字典,或者是否有直接的方式来获取原始请求?
答案 0 :(得分:2)
根据Andrey的建议,我推出了这段代码。它试图重建Web请求,也许这不是获得它的最佳方式,但它是我发现这样做的唯一方法,直到现在。
此程序将显示所请求页面的Web请求(它适用于POST和GET请求):
#!/usr/bin/env python
import web
from urllib import quote
urls = ('(.*)', 'urlhandler')
def adaptHeader(txt):
"""Input: string, header name as it is in web.ctx.env
Output: string, header name according to http protocol.
es: "HTTP_CACHE_CONTROL" => "Cache-Control"
"""
txt = txt.replace('HTTP_', '')
return '-'.join((t[0] + t[1:].lower() for t in txt.split('_')))
def rawRequest(env):
"""Reconstruct and return the web request based on web.ctx.env"""
# url reconstruction
# see http://www.python.org/dev/peps/pep-0333/#url-reconstruction
url = env['wsgi.url_scheme']+'://' # http/https
url += env.get('HTTP_HOST') or (env['SERVER_NAME']+':'+env['SERVER_PORT']) # host + port
url += quote(env.get('SCRIPT_NAME', ''))
url += quote(env.get('PATH_INFO', ''))
url += ('?' + env['QUERY_STRING']) if env.get('QUERY_STRING') else '' # GET querystring
# get/post request
req = ' '.join((env['REQUEST_METHOD'], url, env['SERVER_PROTOCOL'])) + '\n'
# headers
for k, v in env.items():
if k.startswith('HTTP') or k in ('CONTENT_TYPE', 'CONTENT_LENGTH'):
req += adaptHeader(k) + ': ' + str(v) + '\n'
# post data
try:
req += '\n' + env['wsgi.input'].read(int(env['CONTENT_LENGTH']))
except:
pass
return req
class urlhandler:
def GET(self, url):
return rawRequest(web.ctx.env)
def POST(self, url):
return rawRequest(web.ctx.env)
if __name__ == '__main__':
app = web.application(urls, globals())
app.run()
答案 1 :(得分:1)
查看您拥有的内容,您可以使用以“HTTP_”开头的键过滤web.ctx.env
。它比获取和解析原始请求标题更容易。
您可以在此处查看wsgi规范http://www.python.org/dev/peps/pep-0333/#environ-variables
HTTP_变量与客户端提供的HTTP相对应的变量 请求标头(即名称以“HTTP_”开头的变量)。该 这些变量的存在与否应与之相对应 请求中是否存在适当的HTTP标头。