Skip to content

Commit 3501c21

Browse files
committed
fix: use next open port, starting at 8000
1 parent f35be3c commit 3501c21

2 files changed

Lines changed: 21 additions & 3 deletions

File tree

simwrapper/MiniFileServer.py

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import os
44
import re
55
import sys
6+
import socket
67

78
try:
89
# Python3
@@ -117,11 +118,29 @@ def do_OPTIONS(self):
117118
self.end_headers()
118119
return None
119120

121+
def find_free_port(port):
122+
for i in range(port,port+256):
123+
s = socket.socket()
124+
125+
try:
126+
s.bind(('', i))
127+
s.close()
128+
print("Found free port:",i)
129+
return i
130+
131+
except:
132+
pass
133+
finally:
134+
s.close()
135+
120136
def run_mini_file_server(port):
121137
print("\n-----------------------------------------------------------------")
122-
print("SimWrapper-File-Server: " + os.getcwd())
138+
print("SimWrapper file server:", os.getcwd())
139+
140+
free_port = find_free_port(port)
141+
123142
print("-----------------------------------------------------------------\n")
124-
test(HandlerClass=RangeRequestHandler, port=port)
143+
test(HandlerClass=RangeRequestHandler, port=free_port)
125144

126145

127146
if __name__ == '__main__':

simwrapper/cli.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ def cli():
1414
@cli.command(help="Start a local file server in this folder")
1515
@click.argument('port', default=8000)
1616
def serve(port):
17-
click.echo("SERVE! " + str(port))
1817
MiniFileServer.run_mini_file_server(port)
1918

2019
# @cli.command(help="Open your browser and view files in this folder")

0 commit comments

Comments
 (0)