Server code
#!/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()
Clinet
Client send POST request over program Postman.
GET works.
Post is does not work . This is Error on server when I send request
http://192.168.2.108?var1=value
ERROR ON SERVER
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
self.headers.getheader('content-type')
return? – tdelaneyparse_header
is erroring because ofNone
, there is a significant chance that you are passing in aNone
. – tdelaney