从套接字错误python导入模块

时间:2014-12-17 11:18:31

标签: python sockets

当我尝试使用导入套接字模块创建af套接字时:

from socket import *
from thread import *
responseok = bytes('ok')
HOST = ''
PORT = 4445
c = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
c.bind((HOST, PORT))
c.listen(10)

def clientthread(conn):

   dat = conn.recv(1024)
   data = str(dat)
   print data
   conn.close()


while 1:
conn, addr = c.accept()
start_new_thread(clientthread ,(conn,))

c.close()

我收到以下错误:

Traceback (most recent call last):
  File "C:\Users\MikeClaudi\My Documents\LiClipse Workspace\server2\src\socket.py", line 6, in    <module>
from socket import *
  File "C:\Users\MikeClaudi\My Documents\LiClipse Workspace\server2\src\socket.py", line 11, in <module>
c = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

NameError:未定义名称“套接字”

1 个答案:

答案 0 :(得分:1)

如果您使用

from socket import *

然后你必须做

c = socket(socket.AF_INET, socket.SOCK_STREAM)

但为了不混淆两个socket(模块和类),只需

import socket

然后

c = socket.socket(socket.AF_INET, socket.SOCK_STREAM)