Created
June 5, 2012 15:22
-
-
Save PyYoshi/2875637 to your computer and use it in GitHub Desktop.
空きポートを見つける関数 #qiitaのテスト ref: http://qiita.com/items/238a431ff40c22db3d7c
This file contains hidden or 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
| # coding:utf8 | |
| import socket | |
| def find_free_port(): | |
| """ 空きポートを見つける関数 """ | |
| serversock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
| serversock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) | |
| serversock.bind(('127.0.0.1',0)) | |
| address, port = serversock.getsockname() | |
| serversock.close() | |
| return port |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment