继承BaseHTTPRequestHandler 重写 do_XX 和HTTPServer的使用
来源:互联网 发布:ant design pro 知乎 编辑:程序博客网 时间:2024/05/22 03:07
http://www.dongwm.com/archives/guanyu/
http://ju.outofmemory.cn/entry/73142
前言:BaseHTTPServer是一个实现web server的基类
例子1 http get方法:
from BaseHTTPServer import BaseHTTPRequestHandler #BaseHTTPRequestHandler类细分到处理每个协议的方法,这里是‘GET’方法的例子import urlparseclass GetHandler(BaseHTTPRequestHandler): def do_GET(self): #重写这个方法 parsed_path = urlparse.urlparse(self.path) message_parts = [ #建立一个想要返回的列表 'CLIENT VALUES:', #客户端信息 'client_address=%s (%s)' % (self.client_address, self.address_string()), #返回客户端的地址和端口 'command=%s' % self.command, #返回操作的命令,这里比然是'get' 'path=%s' % self.path, #返回请求的路径 'real path=%s' % parsed_path.path, #返回通过urlparse格式化的路径 'query=%s' % parsed_path.query, #返回urlparse格式化的查询语句的键值 'request_version=%s' % self.request_version, #返回请求的http协议版本号 '', 'SERVER VALUES:', #服务器段信息 'server_version=%s' % self.server_version, #返回服务器端http的信息 'sys_version=%s' % self.sys_version, #返回服务器端使用的python版本 'protocol_version=%s' % self.protocol_version, #返回服务器端使用的http协议版本 '', 'HEADERS RECEIVED:', ] for name, value in sorted(self.headers.items()): #返回項添加头信息,包含用户的user-agent信息,主机信息等 message_parts.append('%s=%s' % (name, value.rstrip())) message_parts.append('') message = '\r\n'.join(message_parts) self.send_response(200) #返回给客户端结果,这里的响应码是200 OK,并包含一些其他信息 self.end_headers() #结束头信息 self.wfile.write(message) #返回数据 returnif __name__ == '__main__': from BaseHTTPServer import HTTPServer server = HTTPServer(('localhost', 8080), GetHandler) #在本地8080端口上启用httpserver,使用自定义的GetHandler处理 print 'Starting server, use <Ctrl-C> to stop' server.serve_forever() #保存程序一直运行
客户端测试(linux的curl即可)
dongwm@linux-dongwm:~> curl -i http://localhost:8080/?arg=value HTTP/1.0 200 OK Server: BaseHTTP/0.3 Python/2.7 Date: Mon, 11 Jun 2012 06:59:20 GMT
CLIENT VALUES: client_address=(‘127.0.0.1’, 54016) (localhost) command=GET path=/?arg=value real path=/ query=arg=value request_version=HTTP/1.1
SERVER VALUES: server_version=BaseHTTP/0.3 sys_version=Python/2.7 protocol_version=HTTP/1.0
HEADERS RECEIVED: accept=*/* host=localhost:8080 user-agent=curl/7.21.2 (i686-pc-linux-gnu) libcurl/7.21.2 OpenSSL/1.0.0c zlib/1.2.5 libidn/1.15 libssh2/1.2.7
例子2 http post方法:
from BaseHTTPServer import BaseHTTPRequestHandlerimport cgi #cgi模块,让使用者通过Web服务器来执 行Web程序,并将所执行的结果通过Web服务器返回给浏览器,它产生HTML数据class PostHandler(BaseHTTPRequestHandler): def do_POST(self): #还是重写这个方法 form = cgi.FieldStorage( #cgi.FieldStorage实例效果类似一个字典,包含键-值和len等内置函数 fp=self.rfile, headers=self.headers, environ={'REQUEST_METHOD':'POST', 'CONTENT_TYPE':self.headers['Content-Type'], }) self.send_response(200) self.end_headers() self.wfile.write('Client: %s\n' % str(self.client_address)) self.wfile.write('User-agent: %s\n' % str(self.headers['user-agent'])) self.wfile.write('Path: %s\n' % self.path) self.wfile.write('Form data:\n')
for field in form.keys(): field_item = form[field] self.wfile.write('\t%s=%s\n' % (field, form[field].value)) return
if __name__ == '__main__': from BaseHTTPServer import HTTPServer server = HTTPServer(('localhost', 8080), PostHandler) print 'Starting server, use <Ctrl-C> to stop' server.serve_forever()
客户端测试(linux的curl)
dongwm@linux-dongwm:~> curl http://localhost:8080/ -F name=dongwm -F site=www.dongwm.com Client: (‘127.0.0.1’, 44314) User-agent: curl/7.21.2 (i686-pc-linux-gnu) libcurl/7.21.2 OpenSSL/1.0.0c zlib/1.2.5 libidn/1.15 libssh2/1.2.7 Path: / Form data: name=dongwm site=www.dongwm.com 例子3 使用多线程
注:HTTPServer是SocketServer.TCPServer的一个简单的子类,不能使用多个线程或进程来处理请求,可以这样使用
from BaseHTTPServer import HTTPServer, BaseHTTPRequestHandlerfrom SocketServer import ThreadingMixInimport threadingclass Handler(BaseHTTPRequestHandler):
def do_GET(self): self.send_response(200) self.end_headers() message = threading.currentThread().getName() #换取当前线程的名字 self.wfile.write(message) #返回这个名字 self.wfile.write('\n') return
class ThreadedHTTPServer(ThreadingMixIn, HTTPServer): """Handle requests in a separate thread."""
if __name__ == '__main__': server = ThreadedHTTPServer(('localhost', 8080), Handler) print 'Starting server, use <Ctrl-C> to stop' server.serve_forever()
客户端测试:
dongwm@linux-dongwm:~> curl http://localhost:8080/ Thread-1
dongwm@linux-dongwm:~> curl http://localhost:8080/ Thread-2 dongwm@linux-dongwm:~> curl http://localhost:8080/ Thread-3 dongwm@linux-dongwm:~> curl http://localhost:8080/ Thread-4
- 继承BaseHTTPRequestHandler 重写 do_XX 和HTTPServer的使用
- JDK6 HttpServer的使用
- jdk6 HttpServer的使用
- 类的继承和重写
- BaseHTTPRequestHandler
- python的BaseHTTPRequestHandler的bug
- 继承,多态,重载和重写的区别。
- 静态(static)方法的继承和重写
- 静态(static)方法的继承和重写
- 关于继承和重写的新理解
- java中的继承和方法的重写
- OC类的继承和重写
- 静态(static)方法的继承和重写
- OC里的继承和重写
- Java中类的继承和重写
- python对象的继承和重写
- python对象的继承和重写
- 静态(static)方法的继承和重写
- HDU 4925 Apple Tree(推理)
- 螺旋方阵
- 插入排序
- PHP运算符介绍与使用
- ZOJ-1210
- 继承BaseHTTPRequestHandler 重写 do_XX 和HTTPServer的使用
- winform 五子棋 判断输赢
- hdu 1175 连连看(模拟循环队列)
- POJ3264_Balanced Lineup(线段树/单点更新)
- Python 字符串
- 6.2单链表
- 代理类
- C++ MFC连接mysql
- 黑马程序员_面向对象_继承多态