2013-08-18 72 views
2

我想在我的mac上創建一個SocketServer。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上運行。我的蟒蛇版本是:

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

你爲什麼不將整個回溯 –

+1

看到的,當你提供一個完整的回溯,人可以幫助你馬上 –

+0

@AnttiHaapala感謝您的提示:) – Diolor

回答

5

好像你有SocketServer.py python路徑中的某處。

檢查使用以下命令:

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

重命名該文件將解決你的問題。


UPDATE

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

+1

可能是主要的文件是SocketServer.py –

+0

@AnttiHaapala,你是對的。 – falsetru

+0

謝謝你們兩位。這是文件名。 – Diolor

相關問題