python实现SimpleHTTPServer的POST方法
来源:互联网 发布:怎么找文献的数据 编辑:程序博客网 时间:2024/05/01 13:37
代码如下:
import SimpleHTTPServerimport SocketServerimport redef htc(m): return chr(int(m.group(1),16))def urldecode(url): rex=re.compile('%([0-9a-hA-H][0-9a-hA-H])',re.M) return rex.sub(htc,url)class SETHandler(SimpleHTTPServer.SimpleHTTPRequestHandler): def createHTML(self): html = file("index.html", "r") for line in html: self.wfile.write(line) def do_GET(self): print "GET" print self.headers; self.createHTML() def do_POST(self): print "POST" print self.headers; length = int(self.headers.getheader('content-length')) qs = self.rfile.read(length) url=urldecode(qs) print "url=" print url self.createHTML() Handler = SETHandlerPORT = 8000httpd = SocketServer.TCPServer(("", PORT), Handler)print "serving at port", PORThttpd.serve_forever()
测试用的index.html页面如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>Insert title here</title></head><body><form action="http://127.0.0.1:8000/" method="post">username:<input type="text" name="username"><br>password:<input type="password" name="password"><br> <input type="submit" value="login"></form></body></html>
在浏览器里打开http://127.0.0.1:8000/,输出如下:
GETAccept: */*Accept-Language: zh-cnUser-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; .NET CLR 2.0.50727; .NET CLR 3.0.04506.30; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729; .NET4.0C; .NET4.0E)Accept-Encoding: gzip, deflateHost: 127.0.0.1:8000Connection: Keep-AliveGETAccept: */*Accept-Encoding: gzip, deflateUser-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; .NET CLR 2.0.50727; .NET CLR 3.0.04506.30; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729; .NET4.0C; .NET4.0E)Host: 127.0.0.1:8000Connection: Keep-Alive
点击"login"后,输入如下:
POSTAccept: image/gif, image/jpeg, image/pjpeg, image/pjpeg, application/x-shockwave-flash, application/msword, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/xaml+xml, application/vnd.ms-xpsdocument, application/x-ms-xbap, application/x-ms-application, */*Referer: http://127.0.0.1:8000/Accept-Language: zh-cnUser-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; .NET CLR 2.0.50727; .NET CLR 3.0.04506.30; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729; .NET4.0C; .NET4.0E)Content-Type: application/x-www-form-urlencodedAccept-Encoding: gzip, deflateHost: 127.0.0.1:8000Content-Length: 27Connection: Keep-AliveCache-Control: no-cacheurl=username=yang&password=pass
- python实现SimpleHTTPServer的POST方法
- python SimpleHTTPServer添加POST方法
- Python的SimpleHTTPServer
- Python的SimpleHTTPServer
- python的SimpleHTTPServer
- python的SimpleHTTPServer
- python SimpleHTTPServer的使用
- Python SimpleHTTPServer 实现日志简单的web展示
- Python的SimpleHTTPServer(转)
- Python的http服务(SimpleHTTPServer,BaseHTTPServer,CGIHTTPServer)
- Python的http服务(SimpleHTTPServer,BaseHTTPServer,CGIHTTPServer)
- python SimpleHTTPServer Windows7
- python -m SimpleHTTPServer
- python SimpleHTTPServer 2
- python -m SimpleHTTPServer
- python -m SimpleHTTPServer
- python内置HTTP服务器(SimpleHTTPServer)
- SimpleHTTPServer
- android 游戏中的声音
- socket编程中的超时设置示例详解之二
- WPF后台动态添加TabItem并设置样式
- INS-35354] The system on which you are attempting to install Oracle RAC is not part of a valid clust
- OpenGL入门(一)
- python实现SimpleHTTPServer的POST方法
- Build the seajs project 2: editing the Gruntfile
- ZOJ 2314 Reactor Cooling
- 漫谈JVM的基本垃圾回收算法
- Div+CSS+JS树型菜单
- 使用颜色(color)资源
- linux驱动学习--第一天:设备驱动概述(一)之基础知识
- 再论C++之垃圾回收(GC)
- 黑马程序员_Java基础Day13_String类(Done)