简体   繁体   中英

Python error when I try to run one py file

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

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM