【发布时间】:2014-06-07 03:19:54
【问题描述】:
我在我的python程序中使用了一个端口并关闭它,现在我想再次使用它。只是那个端口而不是另一个端口。
有什么方法可以强制操作系统使用 python 释放端口?
#!/usr/bin/python # This is server.py file
import socket # Import socket module
s = socket.socket() # Create a socket object
host = socket.gethostname() # Get local machine name
port = 12349
portt = 12341 # Reserve a port for your service.
s.bind((host, portt)) # Bind to the port
s.connect((host, port))
s.close
ss = socket.socket() # Create a socket object
host = socket.gethostname() # Get local machine name
ss.bind((host, portt))
s.close
但输出是:
Traceback (most recent call last):
File "client.py", line 17, in <module>
ss.bind((host, portt))
File "/usr/lib/python2.7/socket.py", line 224, in meth
return getattr(self._sock,name)(*args)
socket.error: [Errno 98] Address already in use
【问题讨论】:
-
s.close是一种方法。您可以通过s.close()调用它,否则只会显示有关方法类型的信息。
标签: python sockets network-protocols