有一个打开 UDP套接字
的python程序receiveSock = socket(AF_INET, SOCK_DGRAM)
receiveSock.bind(("", portReceive))
有时会发生程序失败或我在运行时终止它并且无法到达
receiveSock.close()
所以在下次我尝试运行这个程序时,我得到了
receiveSock.bind(("",portReceive))
File "<string>", line 1, in bind
socket.error: [Errno 98] Address already in use
如何使用shell命令(或任何其他有用的想法)关闭此套接字?
答案 0 :(得分:4)
您有两种选择:
try:
# your socket operations
finally:
# close your socket
或者,对于较新版本的Python:
with open_the_socket() as the_socket:
# do stuff with the_socket
当块结束或程序退出时,with statement
将关闭套接字。