错误问题:fd,addr = self._accept()

时间:2019-03-31 09:25:26

标签: python python-3.x tcp

我正在尝试在python中的服务器和客户端之间建立连接。但是程序只是在等待连接之前运行。该程序无法读取地址。

import socket
import sys

host = ''
port = 5131

s = socket.socket()

s.bind((host,port))

s.listen(1)
print('listening')


while 1:

    print('Waiting for a connection......')
    c, addr = s.accept()
    print('Connection established with', addr)


c.close()

结果是这样的:

listening
Waiting for a connection......
Traceback (most recent call last):
  File "/Users/muhrisdham/Downloads/tugas/01.tugas_tcp_server.py", line 32, in <module>
    c, addr = s.accept()
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/socket.py", line 212, in accept
    fd, addr = self._accept()
KeyboardInterrupt

请原谅我的英语,谢谢您

1 个答案:

答案 0 :(得分:0)

我假设程序卡住时必须按下ctrl + C。因此,KeyboardInterrupt。

您编写的是服务器。它的意思是阻塞s.accept()调用,直到对您要监听的地址(0.0.0.0:5131)建立新连接为止。建立新连接后,您将以某种方式处理该连接。

现在剩下要做的就是实现 client

查看本教程:https://pymotw.com/2/socket/tcp.html