简体   繁体   中英

hosting HTML Pages on web server using python

I have a problem when I want to host an HTML file using python on the web server.

I go to cmd and type this in:

python -m http.server

The command works but when I open the domain name I get a list of HTML pages, which I can click and they open. How can I make it so that when I open the domain in chrome it immediately shows me the main.html?

Thank you

it works but when I open it up in chrome it shows me lists of my html files and when I click on one it goes to it.

This is expected behavior.

how can I do it so that when I open it in chrome it immediately shows me the main.html?

Rename main.html to index.html . As docs says standard procedure to handle GET request is

If the request was mapped to a directory, the directory is checked for a file named index.html or index.htm (in that order). If found, the file's contents are returned; otherwise a directory listing is generated by calling the list_directory() method. This method uses os.listdir() to scan the directory, and returns a 404 error response if the listdir() fails.

You can add additional options:

python -m http.server --directory C:/ESD/ --bind 127.0.0.1

--directory will tell python in which folder to look for html files, here i specified the path C:/ESD/

--bind will tell python on which ip the server should run at

If you want to directly open the file instead of the directory listing all html files, you have to rename your main.html to index.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM