python 2.7 - no module named http.server -
here web server class :
import http.server import socketserver class webhandler(http.server.basehttprequesthandler): def parse_post(self): ctype, pdict = cgi.parse_header(self.headers['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['content-length']) postvars = urllib.parse.parse_qs(self.rfile.read(length), keep_blank_values=1) else: postvars = {} return postvars def do_post(self): postvars = self.parse_post() print(postvars) # reply json self.send_response(200) self.send_header("content-type", "application/json") self.send_header("access-control-allow-origin","*"); self.send_header("access-control-expose-headers: access-control-allow-origin"); self.send_header("access-control-allow-headers: origin, x-requested-with, content-type, accept"); self.end_headers() json_response = json.dumps({'test': 42}) self.wfile.write(bytes(json_response, "utf-8"))
when run server got "name 'http' not defined" after import http.server got "no module named http.server"
http.server
exists in python 3. in python 2, should use basehttpserver
module:
from basehttpserver import basehttprequesthandler
should work fine.
Comments
Post a Comment