I'm making a simple web server following the documentaction . The idea is to make the server without install any additional module.
The matter is work perfect in desktop computers but in laptops it shares system32 folder instead the folder where the file is located. I've tried with several desktops and laptops, and always happen the same. Same OS: Windows 10
I don't understand why the result change based on type of computer (and how the program know). Any idea?
I attach the code:
import http.server
import socketserver
import socket
port= 8000
handler = http.server.SimpleHTTPRequestHandler
address_ip = socket.gethostbyname(socket.gethostname())
with socketserver.TCPServer(("", port), handler) as server:
print(f" Server address IP: | {address_ip}:{port} |")
print(f" (Press Ctrl+C to close)\n")
try:
server.serve_forever()
except KeyboardInterrupt:
print("\nServer stoped by user.")
input("\nPress Enter to exit...")
Finally I get a solution, thanks @Marko for the hint.
By some reason in certain computers the python working directory use the one where the script is located and others change to System32. (Possibly something about users rights, although I always execute as Admin.) Whatever.
To correct I've to change the working directory to the one that script is located.
os.chdir(os.path.dirname(os.path.abspath(sys.argv[0])))
With this command it detects the folder of the script and by os.chdir change the directory to the one of the script.