1

I am trying to run a py file and I got the following error

IMPORT ERROR : NO MODULE NAMED "BASEHTTPSERVER"

The code included in py file is the following:

import BaseHTTPServer, SimpleHTTPServer
import ssl
httpd = BaseHTTPServer.HTTPServer(('localhost', 4443), SimpleHTTPServer.SimpleHTTPRequestHandler)
httpd.socket = ssl.wrap_socket (httpd.socket, certfile='server.pem', server_side=True)
httpd.serve_forever()

Thanks in advance Best Regards Alejandro Castan

Alejandro Castan
  • 101
  • 4
  • 13

2 Answers2

1

Answer for Python 3.x If you're using Python3.x change from BaseHTTPServer to from http.server.

If you wrote this code for Python 2.x and you are running it with Python3.x, The 2to3 tool will automatically adapt imports when converting your sources to Python 3.

Answer for Python 2.x The error is telling you that BaseHTTPServer needs to be in your PYTHONPATH.

That is to say, Python cannot find the module BaseHTTPServer anywhere, you either need to install it, or if it is installed in a non-standard location, modify your PYTHONPATH environment variable to include it - however this would be a bit of a strange (though not impossible) situation since that module is normally included in Python2.x

Mike Vella
  • 10,187
  • 14
  • 59
  • 86
  • Hi I am using Python 3x. on the other hand I changed the BaseHTTpSever to http.server the code is the following now "import http.server import ssl httpd = http.server.HTTPServer(('localhost', 4443), http.server.SimpleHTTPRequestHandler) httpd.socket = ssl.wrap_socket(httpd.socket, certfile='server.pem', server_side=True) httpd.serve_forever()" I got the following error filenotfounerror:[Erno 2] no such file or directory. Thanks for your help – Alejandro Castan Sep 07 '13 at 16:02
  • As falsetru said, specify the correct certfile path and accept my or his answer ;) – Mike Vella Sep 07 '13 at 16:10
  • sorry for my ignorance but Could I teach me about how set the correct certfile path? thanks – Alejandro Castan Sep 07 '13 at 16:23
  • see http://stackoverflow.com/questions/14746857/how-to-find-the-path-to-a-ssl-cert-file – Mike Vella Sep 07 '13 at 16:26
1

If you're using Python 3.x, try following:

import http.server
import ssl

httpd = http.server.HTTPServer(('localhost', 4443), http.server.SimpleHTTPRequestHandler)
httpd.socket = ssl.wrap_socket(httpd.socket, certfile='server.pem', server_side=True)
httpd.serve_forever()

BaseHTTPServer, SimpleHTTPServer modules in Python 2 have been merged into http.server module in Python 3.

UPDATE

BTW, port number seems wrong. HTTPS port is 443, not 4443.

falsetru
  • 357,413
  • 63
  • 732
  • 636