1717
1818"""A simple web server for testing purpose.
1919It serves the testing html pages that are needed by the webdriver unit tests."""
20-
20+ import contextlib
2121import logging
2222import os
2323import re
@@ -75,7 +75,7 @@ def do_GET(self):
7575 self .end_headers ()
7676 self .wfile .write (html )
7777 except IOError :
78- self .send_error (404 , ' File Not Found: %s' % path )
78+ self .send_error (404 , f" File Not Found: { path } " )
7979
8080 def do_POST (self ):
8181 """POST method handler."""
@@ -141,7 +141,7 @@ def __init__(self, host=DEFAULT_HOST, port=DEFAULT_PORT):
141141 self .port = port
142142 break
143143 except socket .error :
144- LOGGER .debug ("port %d is in use, trying to next one" % port )
144+ LOGGER .debug (f "port { port } is in use, trying to next one" )
145145 port += 1
146146
147147 self .thread = threading .Thread (target = self ._run_web_server )
@@ -160,14 +160,11 @@ def start(self):
160160 def stop (self ):
161161 """Stops the server."""
162162 self .stop_serving = True
163- try :
164- # This is to force stop the server loop
165- urllib_request .URLopener ().open ("http://%s:%d" % (self .host , self .port ))
166- except IOError :
167- pass
163+ with contextlib .suppress (IOError ):
164+ _ = urllib_request .urlopen (f"http://{ self .host } :{ self .port } " )
168165
169- def where_is (self , path ):
170- return "http://%s:%d/%s" % ( self .host , self .port , path )
166+ def where_is (self , path ) -> str :
167+ return f "http://{ self .host } : { self .port } / { path } "
171168
172169
173170def main (argv = None ):
@@ -180,16 +177,16 @@ def main(argv=None):
180177
181178 parser = OptionParser ("%prog [options]" )
182179 parser .add_option ("-p" , "--port" , dest = "port" , type = "int" ,
183- help = "port to listen (default: %s)" % DEFAULT_PORT ,
180+ help = f "port to listen (default: { DEFAULT_PORT } )" ,
184181 default = DEFAULT_PORT )
185182
186183 opts , args = parser .parse_args (argv [1 :])
187184 if args :
188185 parser .error ("wrong number of arguments" ) # Will exit
189186
190- server = SimpleWebServer (opts .port )
187+ server = SimpleWebServer (port = opts .port )
191188 server .start ()
192- print ("Server started on port %s , hit CTRL-C to quit" % opts . port )
189+ print (f "Server started on port { opts . port } , hit CTRL-C to quit" )
193190 try :
194191 while 1 :
195192 sleep (0.1 )
0 commit comments