gpt4 book ai didi

python - SocketServer 没有模块正在导入

转载 作者:行者123 更新时间:2023-11-28 21:58:26 25 4
gpt4 key购买 nike

我想在我的 mac 上创建一个 SocketServer。

然而,这似乎是软件包的一些问题。当我尝试这个采样代码时发现 here它引发了属性错误。

import SocketServer

class MyTCPHandler(SocketServer.BaseRequestHandler):
"""
The RequestHandler class for our server.

It is instantiated once per connection to the server, and must
override the handle() method to implement communication to the
client.
"""

def handle(self):
# self.request is the TCP socket connected to the client
self.data = self.request.recv(1024).strip()
print "{} wrote:".format(self.client_address[0])
print self.data
# just send back the same data, but upper-cased
self.request.sendall(self.data.upper())

if __name__ == "__main__":
HOST, PORT = "localhost", 9999

# Create the server, binding to localhost on port 9999
server = SocketServer.TCPServer((HOST, PORT), MyTCPHandler)

# Activate the server; this will keep running until you
# interrupt the program with Ctrl-C
server.serve_forever()

错误:

Traceback (most recent call last):
File "/Users/ddl449/Projects/visualization/SocketServer.py", line 1, in <module>
import SocketServer
File "/Users/ddl449/Projects/visualization/SocketServer.py", line 3, in <module>
class MyTCPHandler(SocketServer.BaseRequestHandler):
AttributeError: 'module' object has no attribute 'BaseRequestHandler'

我不知道这是否与我在 Mac 上运行有关。我的 python 版本是:

2.7.5 (v2.7.5:ab05e7dd2788, May 13 2013, 13:18:45) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)]

最佳答案

似乎您在 python 路径中的某处有 SocketServer.py

使用以下命令检查:

python -c "import SocketServer; print(SocketServer.__file__)"

重命名该文件将解决您的问题。


更新

重命名文件 /Users/ddl449/Projects/visualization/SocketServer.py 。如果有 /Users/ddl449/Projects/visualization/SocketServer.pyc,删除该文件。

关于python - SocketServer 没有模块正在导入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18301403/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com