1
我在我的python程序中使用一個端口並關閉它,現在我想再次使用它。只是該端口不是另一個端口。
有什麼辦法強制操作系統與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()'調用它,否則只顯示關於方法類型的信息。 – njzk2