[英]Python error when I try to run one py file
I am trying to run a py file and I got the following error 我正在尝试运行py文件,但出现以下错误
IMPORT ERROR : NO MODULE NAMED "BASEHTTPSERVER"
The code included in py file is the following: py文件中包含的代码如下:
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
. 针对Python 3.x的答案如果您使用的是Python3.x,请将
from BaseHTTPServer
更改为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. 如果您是为Python 2.x编写此代码并与Python3.x一起运行的,则在将源转换为Python 3时, 2to3工具将自动适应导入。
Answer for Python 2.x The error is telling you that BaseHTTPServer
needs to be in your PYTHONPATH
. Python 2.x的答案该错误告诉您
BaseHTTPServer
必须位于您的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 也就是说,Python无法在任何地方找到模块
BaseHTTPServer
,您需要安装它,或者如果它安装在非标准位置,请修改您的PYTHONPATH
环境变量以包含它-但这有点奇怪(尽管并非不可能)的情况,因为该模块通常包含在Python2.x中
If you're using Python 3.x, try following: 如果您使用的是Python 3.x,请尝试以下操作:
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. Python 2中的
BaseHTTPServer
, SimpleHTTPServer
模块已合并到Python 3中的http.server
模块中。
UPDATE UPDATE
BTW, port number seems wrong. 顺便说一句,端口号似乎错误。 HTTPS port is 443, not 4443.
HTTPS端口是443,而不是4443。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.