This module serves as a very quick and easy way to start a local Http server on your network.
Previously in Python 2.7, this module was called
HttpServer . But in Python3 this module has been merged with the
http.server module. Let's get started and run our own Http server.
This module is part of the standard library, so installing you do not need to write it. To import this module, simply use the following statement:
You are now ready to start the server. Now let's write some code for serving files.
If you just want to share your files and directories with another user, you can start the server directly using Python.
Go to any directory you want to share and start the server from there using:
python -m http.server 9000
Here we run our local Http server on port 9000.
Now, to connect to the local server, you need to do the following steps:
arp -aon Windows or
ip -a | grep ineton Linux.
http: // IP_ADDRESS: 9000 /into your browser.
Pay note that you can view the server files or even upload them to the client machine.
Although the default server is handy for direct file sharing, you can configure the server by running a separate file.
For example, we'll run our own Http server that uses
socketserver for TCP communication.
MyHttpRequestHandler calls me do_GET () method for serving the request. To serve a custom file for the request, we can override the function by simply defining a different do_GET () method that returns a different value.
# server.py import http.server # Our http server handler for http requests import soc ketserver # Establish the TCP Socket connections PORT = 9000 class MyHttpRequestHandler (http.server.SimpleHTTPRequestHandler): def do_GET (self): self.path = 'index.html' return http.server.SimpleHTTPRequestHandler.do_GET (self) Handler = MyHttpRequestHandler with socketserver.TCPServer (("", PORT), Handler) as httpd: print ("Http Server Serving at port", PORT) httpd.serve_forever ()
If you name it like
server.py , you can start the http server using:
Since we have defined our custom do_GET () function, we can serve the HTML file of the home page using our server, which in this case is index.html. Also, if the server is running on your system, you can directly access the server using localhost: & lt; portnumber & gt; instead of using IP.
This book is an indispensable guide focuses on Machine Learning and R Programming, in an instructive and conversational tone which helps them who want to make their career in Big Data Analytics/ Data ...
Mark Lutz is the global leader in Python training, author of the oldest and best-selling Python texts, and a pioneer in the Python community since 1992.
Mark Lutz is the author of the found...
Python Workout isn’t designed to teach you Python, although I hope and expect that you’ll learn quite a bit along the way. It is meant to help you improve your understand- ing of Python and how to...
Introduction to Machine Learning with Python: A Guide for Data Scientists 1st Edition. Machine learning has become an integral part of many commercial applications and research projects, but this f...