简体   繁体   English

当我尝试运行一个py文件时出现Python错误

[英]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中的BaseHTTPServerSimpleHTTPServer模块已合并到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.

相关问题 当我尝试运行SendKeys.py时,Python无法识别_sendkeys.c文件 - Python isn't recognizing _sendkeys.c file when I try to run SendKeys.py 当我尝试运行 manage.py runserver 时出现错误 - Get an error when I try to run manage.py runserver 当我尝试运行我的机器人时,为什么会出现这个奇怪的错误? (discord.py) - Why do i get this weird error when i try to run my bot? (discord.py) 当我尝试运行鼻子测试时出现许多python模块错误 - Many python's module error when i try to run nosetests 超出时间限制错误 - 当我尝试运行 Python 3 代码时 - Time limit exceeded Error - When I try to run Python 3 code zsh:当我尝试在 venv 中运行应用程序时,中止 python 错误 - zsh: abort python error when I try to run the app in venv 当我尝试在终端中运行 .py 文件时,它不起作用 - When I try run a .py file in my terminal it doesn’t work [cocos2d-x]当我尝试在Windows 10中运行&#39;python android-build.py -p 19 cpp-tests&#39;时出现错误 - [cocos2d-x]I have an error when I try to run 'python android-build.py -p 19 cpp-tests' in Windows 10 当我在models.py中的python django中运行此命令python manage.py makemigrations博客时出错 - Error when I run this command python manage.py makemigrations blog in python django in models.py 当我尝试运行该程序时,出现以下错误:文件“ python”,第33行,在 <module> NameError:未定义全局名称“ hp” - When i try to run this program i get this error: File “python”, line 33, in <module> NameError: global name 'hp' is not defined
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM