I have the following small python script to run a local server for testing some html:
print('opened')
from http.server import HTTPServer, SimpleHTTPRequestHandler
server_address = ('', 8000)
httpd = HTTPServer(server_address, SimpleHTTPRequestHandler)
print("Listening at https://127.0.0.1:8000/ . . .")
httpd.serve_forever()
When I run this in the terminal, it blocks the print
statements: nothing is printed. But the server works and I can go to localhost:8000
in the browser and access my html files. If, however, I comment out the last line, the call to serve_forever()
, it works, printing both opened
and
Listening at https:127.0.0.1:8000/ . . .
. Except of course it doesn't actually work, since now the server isn't being run.
I find this very confusing. The previous lines are executed before the last line. Why would the last line cause the previous lines to not work?
Python3 on Windows7 if anyone was going to ask, but I doubt that's relevant.