在新的Python 3.x中,BaseHTTPServer, SimpleHTTPServer, CGIHTTPServer整合到http.server包,SocketServer改名为socketserver 在Python中,我们可以使用SimpleHTTPServer包和CGIHTTPServer包来减小以上的负担。其中,SimpleHTTPServer可以用于处理GET方法和HEAD方法的请求。 CGIHTTPServer:使用静态文件或者CGI来回应请求 CGIHTTPServer包中的CGIHTTPRequestHandler类继承自SimpleHTTPRequestHandler类,所以可以用来代替上面的例子 CGIHTTPServer: # A messy HTTP server based on TCP socket import BaseHTTPServer import CGIHTTPServer /usr/bin/env python# Written by Vameiimport cgi form = cgi.FieldStorage() # Output to stdout, CGIHttpServer
**注意,在Python 3.x中,BaseHTTPServer, SimpleHTTPServer, CGIHTTPServer整合到http.server包,SocketServer改名为socketserver 在Python中,我们可以使用SimpleHTTPServer包和CGIHTTPServer包来规定针对不同请求的操作。 CGIHTTPServer:使用静态文件或者CGI来回应请求 CGIHTTPServer包中的CGIHTTPRequestHandler类继承自SimpleHTTPRequestHandler类,所以可以用来代替上面的例子 CGIHTTPServer 先看看什么是CGI (Common Gateway Interface)。CGI是服务器和应用脚本之间的一套接口标准。 CGIHTTPServer HOST = '' PORT = 8000 # Create the server, CGIHTTPRequestHandler is pre-defined handler
二、CGIHTTPServer:使用静态文件或者CGI来回应请求 ? 先看看什么是CGI (Common Gateway Interface)。CGI是服务器和应用脚本之间的一套接口标准。 CGIHTTPServer # Written by Vamei # A messy HTTP server based on TCP socket import BaseHTTPServer import CGIHTTPServer HOST = '' PORT = 8000 # Create the server, CGIHTTPRequestHandler is pre-defined handler server = BaseHTTPServer.HTTPServer((HOST, PORT), CGIHTTPServer.CGIHTTPRequestHandler) # Start the server /usr/bin/env python # Written by Vamei import cgi form = cgi.FieldStorage() # Output to stdout, CGIHttpServer
index.html 则可以直接显示 #简单HTTP 包含GET与HEAD请求 python -m SimpleHTTPServer 8080 #HTTP 包含POST请求 当前 python -m CGIHTTPServer
# # set up and run the server # bind_addr = ('',port) handler = RedirectTestHandler #CGIHTTPServer.CGIHTTPRequestHandler
CGIHTTPServer: 包含处理POST请求和执行CGIHTTPRequestHandler类。 ---- 然而,python3里已经没有这些东西了。
二、CGIHTTPServer:使用静态文件或者CGI来回应请求 ? 先看看什么是CGI (Common Gateway Interface)。CGI是服务器和应用脚本之间的一套接口标准。 # Written by Vamei # A messy HTTP server based on TCP socket import BaseHTTPServer import CGIHTTPServer server, CGIHTTPRequestHandler is pre-defined handler server = BaseHTTPServer.HTTPServer((HOST, PORT), CGIHTTPServer.CGIHTTPRequestHandler /usr/bin/env python # Written by Vamei import cgi form = cgi.FieldStorage() # Output to stdout, CGIHttpServer
CGIHTTPServer: 包含处理POST请求和执行CGIHTTPRequestHandler类。
2.python自带web模块 python自带的web模块包含BaseHTTPServer、SimpleHTTPServer、CGIHTTPServer这三种, 使用python自带的包就能快速搭建起
CGIHTTPServer: 包含处理POST请求和执行CGIHTTPRequestHandler类。 ?
html_parser HTMLParser html.parser http_client httplib http.client BaseHTTPServer BaseHTTPServer http.server CGIHTTPServer CGIHTTPServer http.server SimpleHTTPServer SimpleHTTPServer http.server input raw_input() input() intern six.moves.html_parser import six.moves.http_client import six.moves.BaseHTTPServer import six.moves.CGIHTTPServer
server,则可实现如下类似代码: # -*- coding: utf-8 - try: # Python2 逻辑 import BaseHTTPServer import CGIHTTPServer server = BaseHTTPServer.HTTPServer(('127.0.0.1', 8000), CGIHTTPServer.CGIHTTPRequestHandler) except
imghdr shelve CDROM asynchat imp shlex CGIHTTPServer
httplib sgmllib CDROM asynchat ihooks sha CGIHTTPServer
这个框架包括了BaseHTTPServer , SimpleHTTPServer , CGIHTTPServer , SimpleXMLRPCServer , DocXMLRPCServer 等服务器,
httpserver post python 看到这里,原来改版了:http.server — HTTP servers — Python 3.10.2 documentation 不过把BaseHTTPServer、CGIHTTPServer
CGIHTTPServer: 包含处理POST请求和执行CGIHTTPRequestHandler类。
"$passwd\r" } # "yes/no" { send "yes\r";exp_continue } # } # interact cgi-bin 开启服务器 python -m CGIHTTPServer
= ^= >>= <<= /C7"Common Libs" AST atexit BaseHTTPServer Bastion cmd codecs commands compileall copy CGIHTTPServer
python myScript.py 你想运行免费的python web服务器,以便创建和测试你自己的web页面和cgi脚本,我们可以用python -c命令行开关: # python -c "import CGIHTTPServer ; CGIHTTPServer.test()" --------------------------------------- Serving HTTP on 0.0.0.0 port 8000 .. ---------------------------------- 我们能够在类库中执行作为脚本的模块而不是作为导入的模块,这就是-m参数的动机,现在我们可以这样运行脚本: # python -m CGIHTTPServer