Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

no module named http.server

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"

like image 835
Phani varma Avatar asked Jun 27 '14 05:06

Phani varma


People also ask

How to use Simple http server in python 3?

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.

What does Python HTTP server do?

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.


2 Answers

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) 
like image 135
Sami Avatar answered Sep 29 '22 08:09

Sami


http.server only exists in Python 3. In Python 2, you should use the BaseHTTPServer module:

from BaseHTTPServer import BaseHTTPRequestHandler 

should work just fine.

like image 42
MattDMo Avatar answered Sep 29 '22 07:09

MattDMo