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.
handler
server_forever ? What do you expect ? Add an handler or don't useBase modules
..... – Paletteprint
statements from executing. – Langesys.stdout.flush()
before the lastprint
? – Expugnablepython3 --version
to find out (4) how do you run the script? (e.g. fromcmd.exe
and runpython3 script.py
? or double-click on the script? ...?) – Expugnable-u
erases the problem. I find what you've said profoundly confusing, though-- how can I know what events cause the buffer to release? For instance the programprint('hi') input('hanging on user input')
will print "hi" before the program terminates while it waits for my input. Why is that? – Lange