我在this site上找到了一个脚本,用于通过python命令行运行一个简单的服务器。
我在中添加了一些print
行,因为我想通过命令行为请求打印GET和POST参数,但似乎无法让它们显示在任何地方。
如果我只打印我们的s
变量(pprint (vars(s))
),我最终会看到:
{'client_address': ('127.0.0.1', 53373),
'close_connection': 1,
'command': 'GET',
'connection': <socket._socketobject object at 0x10b6560c0>,
'headers': <mimetools.Message instance at 0x10b689ab8>,
'path': '/favicon.ico',
'raw_requestline': 'GET /favicon.ico HTTP/1.1\r\n',
'request': <socket._socketobject object at 0x10b6560c0>,
'request_version': 'HTTP/1.1',
'requestline': 'GET /favicon.ico HTTP/1.1',
'rfile': <socket._fileobject object at 0x10b6538d0>,
'server': <BaseHTTPServer.HTTPServer instance at 0x10b6893f8>,
'wfile': <socket._fileobject object at 0x10b6536d0>}
然后我尝试对每个索引(pprint (vars(s.connection))
)使用print
命令,但这不起作用。
以下是修改后的脚本:
#!/usr/bin/python
import time
import BaseHTTPServer
from pprint import pprint
HOST_NAME = 'localhost' # !!!REMEMBER TO CHANGE THIS!!!
PORT_NUMBER = 9000 # Maybe set this to 9000.
class MyHandler(BaseHTTPServer.BaseHTTPRequestHandler):
def do_HEAD(s):
s.send_response(200)
s.send_header("Content-type", "text/html")
s.end_headers()
def do_GET(s):
"""Respond to a GET request."""
s.send_response(200)
s.send_header("Content-type", "text/html")
s.end_headers()
s.wfile.write("<html><head><title>Title goes here.</title></head>")
s.wfile.write("<body><form action='.' method='POST'><input name='x' value='1' /><input type='submit' /></form><p>This is a test.</p>")
# If someone went to "http://something.somewhere.net/foo/bar/",
# then s.path equals "/foo/bar/".
s.wfile.write("<p>GET: You accessed path: %s</p>" % s.path)
s.wfile.write("</body></html>")
pprint (vars(s))
def do_POST(s):
"""Respond to a POST request."""
s.send_response(200)
s.send_header("Content-type", "text/html")
s.end_headers()
s.wfile.write("<html><head><title>Title goes here.</title></head>")
s.wfile.write("<body><p>This is a test.</p>")
s.wfile.write("<body><form action='.' method='POST'><input type='text' name='xxxxxxxxxxxx' value='0000000000000000000000' /><input type='submit' /></form><p>This is a test.</p>")
# If someone went to "http://something.somewhere.net/foo/bar/",
# then s.path equals "/foo/bar/".
s.wfile.write("<p>POST: You accessed path: %s</p>" % s.path)
s.wfile.write("</body></html>")
pprint (vars(s))
pprint (vars(s.connection))
pprint (vars(s.headers))
pprint (vars(s.request))
pprint (vars(s.rfile))
pprint (vars(s.server))
pprint (vars(s.wfile))
pprint (vars(s.fp))
"""pprint (vars(s.request))"""
if __name__ == '__main__':
server_class = BaseHTTPServer.HTTPServer
httpd = server_class((HOST_NAME, PORT_NUMBER), MyHandler)
print time.asctime(), "Server Starts - %s:%s" % (HOST_NAME, PORT_NUMBER)
try:
httpd.serve_forever()
except keyboardInterrupt:
pass
httpd.server_close()
print time.asctime(), "Server Stops - %s:%s" % (HOST_NAME, PORT_NUMBER)
如何使用简单的脚本打印POST并获取参数?
通过命令行所需的输出如下所示:
1.0.0.127. - - [03/Oct/2012 16:02:05] "POST / HTTP/1.1" 200 -
foo=1
bar=2
bis=3
这并不十分明显,但处理程序在幕后使用了套接字。所以您需要从套接字读取原始数据,然后对其进行解释。
使用^{} 模块。
urlparse.parse_qs
。urllib.parse.parse_qs
。导入
urlparse
,然后修改do_POST
方法,如下所示:设置简单的测试客户端:
启动服务器,然后运行客户端:
您可以使用
cgi
模块而不是urlparse
。cgi
实现即时后参数分析。使用经过良好测试的库似乎更好。import cgi
相关问题 更多 >
编程相关推荐