here is my 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 with 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 i run server i got "name 'http' is not defined" after import http.server then i got this "no module named http.server"
Python Simple HTTP Server If you are using Windows operating system then go to your desired folder or directory that you want to share. Now, use shift+right click . Your will find option to open command prompt in that directory. Just click on that and open command prompt there.
Python HTTP server is a kind of web server that is used to access the files over the request. Users can request any data or file over the webserver using request, and the server returns the data or file in the form of a response.
In python earlier than v3 you need to run http server as
python -m SimpleHTTPServer 8069 #(8069=portnumber on which your python server is configured)
http.server
only exists in Python 3. In Python 2, you should use the BaseHTTPServer
module:
from BaseHTTPServer import BaseHTTPRequestHandler
should work just fine.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With