2017-02-18 60 views
0

Server代码如何阅读,过程变量POST,Python的HTTP服务器

#!/usr/bin/env python 
import BaseHTTPServer 
from BaseHTTPServer import BaseHTTPRequestHandler 
import SocketServer 
import urlparse 
import cgitb 
import cgi 
from cgi import parse_header, parse_multipart 


class S(BaseHTTPRequestHandler): 
def _set_headers(self): 
    self.send_response(200) 
    self.send_header('Content-type', 'text/html') 
    self.end_headers() 

def do_GET(self): 
    self._set_headers() 
    self.wfile.write("<html><body><h1>hi!</h1></body></html>") 

def do_HEAD(self): 
    self._set_headers() 

def do_POST(self): 
    ctype, pdict = cgi.parse_header(self.headers.getheader('content-type')) 
    if ctype == 'multipart/form-data': 
    postvars = cgi.parse_multipart(self.rfile, pdict) 
    elif ctype == 'application/x-www-form-urlencoded': 
    length = int(self.headers.getheader('content-length')) 
    postvars = cgi.parse_qs(self.rfile.read(length), keep_blank_values=1) 
    else: 
    postvars = {} 

def run(server_class=BaseHTTPServer.HTTPServer, handler_class=S, port=80): 
server_address = ('', port) 
httpd = server_class(server_address, handler_class) 
print 'Starting httpd...' 
httpd.serve_forever() 

if __name__ == "__main__": 

     run() 

客户为例

在程序邮差客户端发送POST请求。

GET的作品。

帖子不起作用。这是在服务器上的错误,当我请求发送

http://192.168.2.108?var1=value

ERROR服务器上

Exception happened during processing of request from ('192.168.2.107', 49629) Traceback (most recent call last): File "/usr/lib/python2.7/SocketServer.py", line 295, in _handle_request_noblock self.process_request(request, client_address) File "/usr/lib/python2.7/SocketServer.py", line 321, in process_request self.finish_request(request, client_address) File "/usr/lib/python2.7/SocketServer.py", line 334, in finish_request self.RequestHandlerClass(request, client_address, self) File "/usr/lib/python2.7/SocketServer.py", line 655, in init self.handle() File "/usr/lib/python2.7/BaseHTTPServer.py", line 340, in handle self.handle_one_request() File "/usr/lib/python2.7/BaseHTTPServer.py", line 328, in handle_one_request method() File "server.py", line 37, in do_POST ctype, pdict = cgi.parse_header(self.headers.getheader('content-type')) File "/usr/lib/python2.7/cgi.py", line 309, in parse_header parts = _parseparam(';' + line) TypeError: cannot concatenate 'str' and 'NoneType' objects

+0

什么'self.headers.getheader('content-type')'返回? – tdelaney

+0

为什么你让异常文本再次无法读取?您可以花时间打印内容类型,以查看它是否真的在标题中或返回None,而不是难以读取异常。如果'parse_header'由于'None'而出错,那么很有可能你传入了'None'。 – tdelaney

+0

谢谢你tdelaney。问题出在邮差。 可能我设置了错误的值。发布在Android Studio – user7432329

回答

0

也许是因为你的客户端发送请求不包括Content-Type头。

你没有说你如何连接到服务器。

curl -X POST http://localhost:8880/?var1=value 

将失败,如上所示。虽然:

curl -X POST http://localhost:8880/?var1=value -H 'Content-type: application/x-www-form-urlencoded' 

也将失败,但有点沿途进一步:你还需要一个内容长度头。你需要的是检查返回给你的值类方法,而不是相信一切都会发生,因为你希望它会:

def do_POST(self): 
    content_type = self.headers.getheader('content-type') 
    if content_type is None: 
     raise ... # 400 Invalid Request 

    # otherwise go on. 
+0

感谢您的建议,它是解决方案。但我不知道为什么content-lenght是空的。 **如果content_lenght为None:print“empty”** – user7432329

+0

@ user7432329也许是因为当Content-type是application/x-www-form-urlencoded时,查询应该包含一个body(参见curl的手册页特别是它的'-d'开关)。这就是为什么你需要使用'POST'而不是'GET'方法。 – Cans