Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to build a local site with Python code

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces "how to use Python code to build a local site". In daily operation, I believe many people have doubts about how to use Python code to build a local site. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubts of "how to use Python code to build a local site". Next, please follow the editor to study!

I have a lot of PDF learning materials in my computer, hundreds of which are related to Python.

It is troublesome to read every time, and the browser can read the PDF file, so the idea of moving the PDF file to the browser to read is just like browsing the web.

It is also very simple to implement with Python, and 8 lines of code is done. Paste the code first:

Import os

From http.server import HTTPServer,CGIHTTPRequestHandler

Webdir ='.'# set the root directory of the website to the path where the program is located

Port = 80 # set a port

Os.chdir (webdir)

Server_address = ('', port) # set the server address

Server_obj = HTTPServer (server_address,CGIHTTPRequestHandler) # create server object

Server_obj.serve_forever () # start the server

We have set the root directory of the website as the path where the program is located, so as long as we run the above program, the folder where the program is located will become the local site and can be browsed through the browser. Let's do this:

1. Run the program

As can be seen in the above dynamic chart, there is no response after the program is run, and no response is a good thing, which represents a successful operation. Then he will record your actions in the browser.

two。 Browsing method

Open your browser, enter 127.0.0.1 or localhost enter, and you can browse the entire folder in the browser.

There are two types of directories displayed in the browser interface:

The file with the suffix name, click to open it directly. With / for the folder, click to enter.

All the requests we made to the browser were recorded:

At this point, the study on "how to build a local site with Python code" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report