由于socket.py中的错误,无法设置服务器

时间:2019-07-11 11:23:51

标签: python-3.x

import socket

print(socket.gethostbyname(socket.gethostname()));
TCP_IP = "192.168.56.1";
TCP_PORT = 8080;
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM);
s.bind((TCP_IP, TCP_PORT));


conn, addr = s.accept();
print('Connection address:' + addr);
while 1:
    print("Started: ");
    data = conn.recv(20);
    if not data: break
    print ("received data:", data);
    conn.send(data);
conn.close();

考虑以下代码,我正在尝试在Python上设置WIFi TCP服务器。但是由于某种原因,运行输出时,我得到的是:

Traceback (most recent call last):
  File "C:/Users/Yoga/PycharmProjects/untitled/exec", line 10, in <module>
    conn, addr = s.accept();
  File "C:\Users\Yoga\AppData\Local\Programs\Python\Python37-32\lib\socket.py", line 212, in accept
    fd, addr = self._accept()
OSError: [WinError 10022] An invalid argument was supplied

显然,socket.py中存在某种错误。欢迎提出如何修复的建议。

1 个答案:

答案 0 :(得分:1)

s.bind()之后和s.accept()之前,您必须致电s.listen()一次。

在官方文档中查看Echo example