Created
June 16, 2012 07:55
-
-
Save laclefyoshi/2940414 to your computer and use it in GitHub Desktop.
get unused port
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
>>> import SocketServer | |
>>> ss = SocketServer.TCPServer(('localhost', 8080), None) | |
Traceback (most recent call last): | |
File "<stdin>", line 1, in <module> | |
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/SocketServer.py", line 408, in __init__ | |
self.server_bind() | |
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/SocketServer.py", line 419, in server_bind | |
self.socket.bind(self.server_address) | |
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/socket.py", line 224, in meth | |
return getattr(self._sock,name)(*args) | |
socket.error: [Errno 48] Address already in use | |
>>> import socket | |
>>> sock = sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
>>> sock.bind(('localhost', 0)) | |
('127.0.0.1', 65284) | |
>>> unused_port = _[1] # 65284 | |
>>> sock.close() | |
>>> ss = SocketServer.TCPServer(('localhost', unused_port), None) | |
>>> ss.serve_forever() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment