0
votes

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

1
What does self.headers.getheader('content-type') return?tdelaney
Why did you make the exception text unreadable again? Instead of making it hard to read the exception you could spend your time printing the content-type to see if its really in the header or returning None. If parse_header is erroring because of None, there is a significant chance that you are passing in a None.tdelaney
Thank you tdelaney. Problem was in Postman. probably I set wrong values. Post works on Android studiouser7432329

1 Answers

0
votes

Maybe because the request sent by your client does not include the content-type header.

You did not say how you are connecting to the server.

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

Will fail as shown above. While:

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

Will fail also, but a bit further along the way: you also need a Content-length header. What you need is to check the values returned to you by the class methods instead of trusting everything will happen as you wish it will:

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

    # otherwise go on.