2013-08-18 3 views
2

내 Mac에서 SocketServer를 만들고 싶습니다.SocketServer 모듈 가져 오기가 없습니다.

그러나 패키지에 문제가있는 것 같습니다. 이 샘플 코드를 시도하면 here이 발견되어 attributeerror가 발생합니다.

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

페이지를 복사하지 마십시오 아마 주 파일이 SocketServer.py –

+0

@AnttiHaapala가 팁 : – Diolor

답변

5

어딘가에 파이썬 경로에 SocketServer.py이있는 것 같습니다. 다음 명령을 사용하여

확인 :

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

문제를 해결할 것입니다 해당 파일의 이름을 변경.


UPDATE 파일 /Users/ddl449/Projects/visualization/SocketServer.py 이름을 바꿉니다. /Users/ddl449/Projects/visualization/SocketServer.pyc이 있으면 해당 파일을 제거하십시오.

+1

에 대한 감사합니다 당신을 도울 수, 전체 역 추적을 –

+0

@AnttiHaapala이며, 그래 네가 맞아. – falsetru

+0

감사합니다. 그것은 파일 이름이었습니다. – Diolor

관련 문제