File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 33import os
44import re
55import sys
6+ import socket
67
78try :
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+
120136def 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
127146if __name__ == '__main__' :
Original file line number Diff line number Diff 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 )
1616def 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")
You can’t perform that action at this time.
0 commit comments