#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""
Created on 2015-7-20
@author: xhw
@explain: 实现GET方法和POST方法请求
"""
from BaseHTTPServer import HTTPServer, BaseHTTPRequestHandler
import urllib
class HTTPRequestHandler(BaseHTTPRequestHandler):
def do_GET(self):
print 'self.path: %s' % self.path
path, parm = urllib.splitquery(self.path)
print 'path: %s\nparm: %s' % (path, parm)
self.send_response(200)
self.send_header("Content-type", "text/html")
self.send_header("test", "This is test!")
self.end_headers()
buf = '''<!DOCTYPE HTML>
<html>
<head><title>Get page</title></head>
<body>
<form action="post_page" method="post">
username: <input type="text" name="username" /><br />
password: <input type="text" name="password" /><br />
<input type="submit" value="POST" />
</form>
</body>
</html>'''
self.wfile.write(buf)
def do_POST(self):
print self.path
# 获取post数据
content_length = int(self.headers['Content-Length']) # Gets the size of data
datas = self.rfile.read(content_length)
datas = urllib.unquote(datas).decode("utf-8", 'ignore')
self.send_response(200)
self.send_header("Content-type", "text/html")
# self.send_header("Content-type", "application/json; charset=utf-8")
buf = '''<!DOCTYPE HTML>
<html>
<head><title>Post page</title></head>
<body>Post Data:%s <br />Path:%s</body>
</html>''' % (datas, self.path)
self.send_header("Content-Length", len(buf))
self.wfile.write(buf)
self.end_headers()
# "C:\Python27\Lib\BaseHTTPServer.py"
def start_server(port):
server_address = ('', port)
httpd = HTTPServer(server_address, HTTPRequestHandler)
httpd.serve_forever()
if __name__ == "__main__":
start_server(8000)
在浏览器输入:http://127.0.0.1:8000 即可出现界面。
网上看了一下很多简易的httpserver没什么实际价值,自己看了很多github大神写的demo特地总结一下使用。
先贴代码
from http.server import BaseHTTPRequestHandler, HTTPServer
import logging
class S(BaseHTTPRequestHandler):
def do_HEAD(self):
self.send_response(200)
self.send_header('Content-type', 'text/html')
self.end_headers()
def do_GET(self):
paths = {
'/foo': {'status': 200},
'/bar': {'status': 302},
'/baz': {'status': 404},
'/qux': {'status': 500}
}
if self.path in paths:
self.respond(paths[self.path])
else:
self.respond({'status': 500})
logging.info("GET request,\nPath: %s\nHeaders:\n%s\n", str(self.path), str(self.headers))
self.wfile.write("GET request for {}".format(self.path).encode('utf-8'))
def do_POST(self):
content_length = int(self.headers['Content-Length']) # <--- Gets the size of data
post_data = self.rfile.read(content_length) # <--- Gets the data itself
logging.info("POST request,\nPath: %s\nHeaders:\n%s\n\nBody:\n%s\n",
str(self.path), str(self.headers), post_data.decode('utf-8'))
self.do_HEAD()
self.wfile.write("POST request for {}".format(self.path).encode('utf-8'))
def respond(self, opts):
response = self.handle_http(opts['status'], self.path)
self.wfile.write(response)
def handle_http(self, status_code, path):
self.send_response(status_code)
self.send_header('Content-type', 'text/html')
self.end_headers()
content = '''
<html><head><title>Title goes here.</title></head>
<body><p>This is a test.</p>
<p>You accessed path: {}</p>
</body></html>
'''.format(path)
return bytes(content, 'UTF-8')
def run(server_class=HTTPServer, handler_class=S, port=8080):
logging.basicConfig(level=logging.INFO)
server_address = ('', port)
httpd = server_class(server_address, handler_class)
logging.info('Starting httpd...\n')
try:
httpd.serve_forever()
except KeyboardInterrupt:
pass
httpd.server_close()
logging.info('Stopping httpd...\n')
if __name__ == '__main__':
from sys import argv
if len(argv) == 2:
run(port=int(argv[1]))
else:
run()
在run()方法中我们可以看到我们实例化一个HTTPServer对象,这个HTTPServer的源码是这样的:
class HTTPServer(socketserver.TCPServer):
allow_reuse_address = 1 # Seems to make sense in testing environment
def server_bind(self):
"""Override server_bind to store the server name."""
socketserver.TCPServer.server_bind(self)
host, port = self.socket.getsockname()[:2]
self.server_name = socket.getfqdn(host)
self.server_port = port
可以看到他就一个bind方法,继承了TCPServer,所以让我们看看TCPServer的源码
class TCPServer(BaseServer):
"""Base class for various socket-based server classes.
Defaults to synchronous IP stream (i.e., TCP).
Methods for the caller:
- __init__(server_address, RequestHandlerClass, bind_and_activate=True)
- serve_forever(poll_interval=0.5)
- shutdown()
- handle_request() # if you don't use serve_forever()
- fileno() -> int # for selector
Methods that may be overridden:
- server_bind()
- server_activate()
- get_request() -> request, client_address
- handle_timeout()
- verify_request(request, client_address)
- process_request(request, client_address)
- shutdown_request(request)
- close_request(request)
- handle_error()
Methods for derived classes:
- finish_request(request, client_address)
Class variables that may be overridden by derived classes or
instances:
- timeout
- address_family
- socket_type
- request_queue_size (only for stream sockets)
- allow_reuse_address
Instance variables:
- server_address
- RequestHandlerClass
- socket
"""
address_family = socket.AF_INET
socket_type = socket.SOCK_STREAM
request_queue_size = 5
allow_reuse_address = False
def __init__(self, server_address, RequestHandlerClass, bind_and_activate=True):
"""Constructor. May be extended, do not override."""
BaseServer.__init__(self, server_address, RequestHandlerClass)
self.socket = socket.socket(self.address_family,
self.socket_type)
if bind_and_activate:
try:
self.server_bind()
self.server_activate()
except:
self.server_close()
raise
def server_bind(self):
"""Called by constructor to bind the socket.
May be overridden.
"""
if self.allow_reuse_address:
self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
self.socket.bind(self.server_address)
self.server_address = self.socket.getsockname()
def server_activate(self):
"""Called by constructor to activate the server.
May be overridden.
"""
self.socket.listen(self.request_queue_size)
def server_close(self):
"""Called to clean-up the server.
May be overridden.
"""
self.socket.close()
def fileno(self):
"""Return socket file number.
Interface required by selector.
"""
return self.socket.fileno()
def get_request(self):
"""Get the request and client address from the socket.
May be overridden.
"""
return self.socket.accept()
def shutdown_request(self, request):
"""Called to shutdown and close an individual request."""
try:
#explicitly shutdown. socket.close() merely releases
#the socket and waits for GC to perform the actual close.
request.shutdown(socket.SHUT_WR)
except OSError:
pass #some platforms may raise ENOTCONN here
self.close_request(request)
def close_request(self, request):
"""Called to clean up an individual request."""
request.close()
def **init**(self, server_address, RequestHandlerClass, bind_and_activate=True):
这个构造函数里面要求传入一个地址和一个类,这里就不深究BaseServer了,这里我们注意传入的RequestHandlerClass这个类,通常是继承BaseHTTPRequestHandler,当然很多继承了
SimpleHTTPRequestHandler,这个类也是继承了BaseHTTPRequestHandler,不过提供了get方法,HTTPServer实例创建完毕调用serve_forever()就可以一直运行了
下面看看这几个方法 do_POST(),这个方法什么时候调用呢?
class BaseHTTPRequestHandler(socketserver.StreamRequestHandler):
"""HTTP request handler base class.
The following explanation of HTTP serves to guide you through the
code as well as to expose any misunderstandings I may have about
HTTP (so you don't need to read the code to figure out I'm wrong
:-).
HTTP (HyperText Transfer Protocol) is an extensible protocol on
top of a reliable stream transport (e.g. TCP/IP). The protocol
recognizes three parts to a request:
1. One line identifying the request type and path
2. An optional set of RFC-822-style headers
3. An optional data part
The headers and data are separated by a blank line.
The first line of the request has the form
<command> <path> <version>
where <command> is a (case-sensitive) keyword such as GET or POST,
<path> is a string containing path information for the request,
and <version> should be the string "HTTP/1.0" or "HTTP/1.1".
<path> is encoded using the URL encoding scheme (using %xx to signify
the ASCII character with hex code xx).
The specification specifies that lines are separated by CRLF but
for compatibility with the widest range of clients recommends
servers also handle LF. Similarly, whitespace in the request line
is treated sensibly (allowing multiple spaces between components
and allowing trailing whitespace).
Similarly, for output, lines ought to be separated by CRLF pairs
but most clients grok LF characters just fine.
If the first line of the request has the form
<command> <path>
(i.e. <version> is left out) then this is assumed to be an HTTP
0.9 request; this form has no optional headers and data part and
the reply consists of just the data.
The reply form of the HTTP 1.x protocol again has three parts:
1. One line giving the response code
2. An optional set of RFC-822-style headers
3. The data
Again, the headers and data are separated by a blank line.
The response code line has the form
<version> <responsecode> <responsestring>
where <version> is the protocol version ("HTTP/1.0" or "HTTP/1.1"),
<responsecode> is a 3-digit response code indicating success or
failure of the request, and <responsestring> is an optional
human-readable string explaining what the response code means.
This server parses the request and the headers, and then calls a
function specific to the request type (<command>). Specifically,
a request SPAM will be handled by a method do_SPAM(). If no
such method exists the server sends an error response to the
client. If it exists, it is called with no arguments:
do_SPAM()
Note that the request name is case sensitive (i.e. SPAM and spam
are different requests).
The various request details are stored in instance variables:
- client_address is the client IP address in the form (host,
port);
- command, path and version are the broken-down request line;
- headers is an instance of email.message.Message (or a derived
class) containing the header information;
- rfile is a file object open for reading positioned at the
start of the optional input data part;
- wfile is a file object open for writing.
IT IS IMPORTANT TO ADHERE TO THE PROTOCOL FOR WRITING!
The first thing to be written must be the response line. Then
follow 0 or more header lines, then a blank line, and then the
actual data (if any). The meaning of the header lines depends on
the command executed by the server; in most cases, when data is
returned, there should be at least one header line of the form
Content-type: <type>/<subtype>
where <type> and <subtype> should be registered MIME types,
e.g. "text/html" or "text/plain".
"""
# The Python system version, truncated to its first component.
sys_version = "Python/" + sys.version.split()[0]
# The server software version. You may want to override this.
# The format is multiple whitespace-separated strings,
# where each string is of the form name[/version].
server_version = "BaseHTTP/" + __version__
error_message_format = DEFAULT_ERROR_MESSAGE
error_content_type = DEFAULT_ERROR_CONTENT_TYPE
# The default request version. This only affects responses up until
# the point where the request line is parsed, so it mainly decides what
# the client gets back when sending a malformed request line.
# Most web servers default to HTTP 0.9, i.e. don't send a status line.
default_request_version = "HTTP/0.9"
def parse_request(self):
"""Parse a request (internal).
The request should be stored in self.raw_requestline; the results
are in self.command, self.path, self.request_version and
self.headers.
Return True for success, False for failure; on failure, an
error is sent back.
"""
self.command = None # set in case of error on the first line
self.request_version = version = self.default_request_version
self.close_connection = True
requestline = str(self.raw_requestline, 'iso-8859-1')
requestline = requestline.rstrip('\r\n')
self.requestline = requestline
words = requestline.split()
if len(words) == 3:
command, path, version = words
if version[:5] != 'HTTP/':
self.send_error(
HTTPStatus.BAD_REQUEST,
"Bad request version (%r)" % version)
return False
try:
base_version_number = version.split('/', 1)[1]
version_number = base_version_number.split(".")
# RFC 2145 section 3.1 says there can be only one "." and
# - major and minor numbers MUST be treated as
# separate integers;
# - HTTP/2.4 is a lower version than HTTP/2.13, which in
# turn is lower than HTTP/12.3;
# - Leading zeros MUST be ignored by recipients.
if len(version_number) != 2:
raise ValueError
version_number = int(version_number[0]), int(version_number[1])
except (ValueError, IndexError):
self.send_error(
HTTPStatus.BAD_REQUEST,
"Bad request version (%r)" % version)
return False
if version_number >= (1, 1) and self.protocol_version >= "HTTP/1.1":
self.close_connection = False
if version_number >= (2, 0):
self.send_error(
HTTPStatus.HTTP_VERSION_NOT_SUPPORTED,
"Invalid HTTP Version (%s)" % base_version_number)
return False
elif len(words) == 2:
command, path = words
self.close_connection = True
if command != 'GET':
self.send_error(
HTTPStatus.BAD_REQUEST,
"Bad HTTP/0.9 request type (%r)" % command)
return False
elif not words:
return False
else:
self.send_error(
HTTPStatus.BAD_REQUEST,
"Bad request syntax (%r)" % requestline)
return False
self.command, self.path, self.request_version = command, path, version
# Examine the headers and look for a Connection directive.
try:
self.headers = http.client.parse_headers(self.rfile,
_class=self.MessageClass)
except http.client.LineTooLong:
self.send_error(
HTTPStatus.BAD_REQUEST,
"Line too long")
return False
except http.client.HTTPException as err:
self.send_error(
HTTPStatus.REQUEST_HEADER_FIELDS_TOO_LARGE,
"Too many headers",
str(err)
)
return False
conntype = self.headers.get('Connection', "")
if conntype.lower() == 'close':
self.close_connection = True
elif (conntype.lower() == 'keep-alive' and
self.protocol_version >= "HTTP/1.1"):
self.close_connection = False
# Examine the headers and look for an Expect directive
expect = self.headers.get('Expect', "")
if (expect.lower() == "100-continue" and
self.protocol_version >= "HTTP/1.1" and
self.request_version >= "HTTP/1.1"):
if not self.handle_expect_100():
return False
return True
def handle_expect_100(self):
"""Decide what to do with an "Expect: 100-continue" header.
If the client is expecting a 100 Continue response, we must
respond with either a 100 Continue or a final response before
waiting for the request body. The default is to always respond
with a 100 Continue. You can behave differently (for example,
reject unauthorized requests) by overriding this method.
This method should either return True (possibly after sending
a 100 Continue response) or send an error response and return
False.
"""
self.send_response_only(HTTPStatus.CONTINUE)
self.end_headers()
return True
def handle_one_request(self):
"""Handle a single HTTP request.
You normally don't need to override this method; see the class
__doc__ string for information on how to handle specific HTTP
commands such as GET and POST.
"""
try:
self.raw_requestline = self.rfile.readline(65537)
if len(self.raw_requestline) > 65536:
self.requestline = ''
self.request_version = ''
self.command = ''
self.send_error(HTTPStatus.REQUEST_URI_TOO_LONG)
return
if not self.raw_requestline:
self.close_connection = True
return
if not self.parse_request():
# An error code has been sent, just exit
return
mname = 'do_' + self.command
if not hasattr(self, mname):
self.send_error(
HTTPStatus.NOT_IMPLEMENTED,
"Unsupported method (%r)" % self.command)
return
method = getattr(self, mname)
method()
self.wfile.flush() #actually send the response if not already done.
except socket.timeout as e:
#a read or a write timed out. Discard this connection
self.log_error("Request timed out: %r", e)
self.close_connection = True
return
def handle(self):
"""Handle multiple requests if necessary."""
self.close_connection = True
self.handle_one_request()
while not self.close_connection:
self.handle_one_request()
def handle_one_request(self): 这个函数里面有一句
mname = 'do_' + self.command
if not hasattr(self, mname):
self.send_error(
HTTPStatus.NOT_IMPLEMENTED,
"Unsupported method (%r)" % self.command)
return
method = getattr(self, mname)
method()
self.command是什么呢?猜猜也就知道是get或者post请求了,具体可以看看这几行代码
self.command = None # set in case of error on the first line
self.request_version = version = self.default_request_version
self.close_connection = True
requestline = str(self.raw_requestline, 'iso-8859-1')
requestline = requestline.rstrip('\r\n')
self.requestline = requestline
words = requestline.split()
if len(words) == 3:
command, path, version = words
下面就执行do_POST或者do_GET方法,这点没有什么疑问了吧
接着上一篇文章,这里介绍 do_POST方法该怎么玩
def do_POST(self):
content_length = int(self.headers['Content-Length']) # <--- Gets the size of data
post_data = self.rfile.read(content_length) # <--- Gets the data itself
logging.info("POST request,\nPath: %s\nHeaders:\n%s\n\nBody:\n%s\n",
str(self.path), str(self.headers), post_data.decode('utf-8'))
self.do_HEAD()
self.wfile.write("POST request for {}".format(self.path).encode('utf-8'))
这里我们首先获取了content_length这个值。下面介绍下http工作原理。
首先你得知道tcp是什么,一般来说,tcp连接建立好之后我们就可以互相发送消息了,http也是一样先建立一个tcp连接,然后发消息,不过它发消息是有格式的,一般是这样的
POST /reg.jsp HTTP/ (CRLF)
Accept:image/gif,image/x-xbit,... (CRLF)
...
HOST:127.0.0.1:8080 (CRLF)
Content-Length:22 (CRLF)
Connection:Keep-Alive (CRLF)
Cache-Control:no-cache (CRLF)
(CRLF) //该CRLF表示消息报头已经结束,在此之前为消息报头
user=jeffrey&pwd=1234 //此行以下为提交的数据
我们发送post请求如下:http://127.0.0.1:8080/reg.jsp
这时候我们发过去的就是上面的内容,这里的 Content-Length:22 指的是 user=jeffrey&pwd=1234 这个内容长度,为啥要用这个值呢?这是因为怕数据没有一次发过来,我们可以通过长度来知道继续接收。
就补充这么多,要解释的很多如果细说的话,比如(CRLF)这里指的是/r/n,为啥这个格式要换行,其实说来说去都是为了方便解析,就说这么多了,知道后我们继续讲 do_POST,post_data = self.rfile.read(content_length),这个是读取body体里面的内容就是 user=jeffrey&pwd=1234,我们得知道他的长度所以前面我们获取了长度self.do_HEAD(),这里do_HEAD名字可以乱取,你用do_response,也可以,主要就是设置
def do_HEAD(self):
self.send_response(200)
self.send_header('Content-type', 'text/html')
self.end_headers()
请求头的比如你用上面的返回的就是
HTTP/1.1 200 OK (CRLF)
Content-Type: text/htm (CRLF)
...
(CRLF)
POST request for /
这里self.wfile.write("POST request for {}".format(self.path).encode('utf-8'))
就是设置上面POST request for /
你换成self.wfile.write("123456789")
HTTP/1.1 200 OK (CRLF)
Content-Type: text/htm (CRLF)
...
(CRLF)
123456789
这个就是对方收到的信息,这样你想给对面发什么就可以调用,self.wfile.write("") 给对面发送消息
do_GET我就简单提一点,GET和POST区别是他没有body体,他所有的都是在url中传入的。比如发送get请求 http://127.0.0.1:8080?user=jeffrey&pwd=1234,收到就是
GET /?user=jeffrey&pwd=1234 HTTP/1.1..
Host: 127.0.0.1:8080..
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:60.0)
...
我们要的值就在/?user=jeffrey&pwd=1234这里,GET没有请求体的,当然我们回复都是post回复的,我们把回复的内容放在body里面,至于有没有get回复,我看这个Python都是利用post回复的