X

Python TCP Socket Server 遇到 socket 沒有 AF_INET 問題

Python 要建立 TCP 的 Socket server 臨時存取用,但是卻遇到下述錯誤:

  • AttributeError: partially initialized module 'socket' has no attribute 'AF_INET' (most likely due to a circular import)

Python TCP Socket Server 遇到 socket 沒有 AF_INET 問題

  1. vim /tmp/socket.py # 內容如下: (註:這只是簡單的 TCP Server,內容都不是重點)
    #!usr/bin/python3
    # -- coding: utf-8 --                                                                                                                                                                         
    import socket                                                                                                                                                                                 
                                                                                                                                                                                                  
    HOST = '0.0.0.0'                                                                                                                                                                              
    PORT = 9000                                                                                                                                                                                   
                                                                                                                                                                                                  
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)                                                                                                                                         
    s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)                                                                                                                                       
    s.bind((HOST, PORT))                                                                                                                                                                          
    s.listen(5)                                                                                                                                                                                   
                                                                                                                                                                                                  
    print('server start at: %s:%s' % (HOST, PORT))                                                                                                                                                
    print('wait for connection...')                                                                                                                                                               
                                                                                                                                                                                                  
    while True:                                                                                                                                                                                   
        conn, addr = s.accept()                                                                                                                                                                   
        print('connected by ' + str(addr))                                                                                                                                                        
                                                                                                                                                                                                  
        while True:                                                                                                                                                                               
            indata = conn.recv(1024)                                                                                                                                                              
            if len(indata) == 0: # connection closed                                                                                                                                              
                conn.close()                                                                                                                                                                      
                print('client closed connection.')                                                                                                                                                
                break                                                                                                                                                                             
            print('recv: ' + indata.decode())                                                                                                                                                     
                                                                                                                                                                                                  
            outdata = 'echo ' + indata.decode()                                                                                                                                                   
            conn.send(outdata.encode())                                                                                                                                                           
    s.close()                                                                                                                                                                                     
    
  2. vim /tmp/socket.py # 內容如下: (註:這只是簡單的 TCP Server,內容都不是重點)
  3. $ ./socket.py
    Traceback (most recent call last):
        File "/tmp/./socket.py", line 3, in <module>
            import socket
        File "/tmp/tcp/socket.py", line 8, in <module>
            s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    AttributeError: partially initialized module 'socket' has no attribute 'AF_INET' (most likely due to a circular import)
  4. 上面程式都是對的,遇到錯誤訊息... 最後發現... 是檔名問題...
  5. my socket.py mysocket.py # 再 ./mysocket.py 就可以正常執行了
  6. 因為 import socket ... 跟 socket.py 的檔名問題,但是通常這種都是臨時建立的,所以很容易遇到這種蠢問題
Tsung: 對新奇的事物都很有興趣, 喜歡簡單的東西, 過簡單的生活.
Related Post