{"id":6338,"date":"2020-06-18T14:36:43","date_gmt":"2020-06-18T14:36:43","guid":{"rendered":"https:\/\/www.askpython.com\/?p=6338"},"modified":"2020-06-18T14:36:45","modified_gmt":"2020-06-18T14:36:45","slug":"python-httpserver","status":"publish","type":"post","link":"https:\/\/www.askpython.com\/python-modules\/python-httpserver","title":{"rendered":"Using Python HttpServer as a simple HTTP Server"},"content":{"rendered":"\n<p>Hello everyone! In today&#8217;s article, we&#8217;ll take a look at using Python HttpServer.<\/p>\n\n\n\n<p>This module serves as a very quick and easy way to start a local Http Server on your network.<\/p>\n\n\n\n<p>Earlier, in Python 2.7, this module was called <code>HttpServer<\/code>. But with Python3, this module has been merged into the <code>http.server<\/code> module.<\/p>\n\n\n\n<p>Let&#8217;s get started, and run our own Http Server!<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Importing Python httpserver Module<\/h2>\n\n\n\n<p>This module comes as part of the standard library, so there&#8217;s no need to <a href=\"https:\/\/www.askpython.com\/python-modules\/python-pip\" class=\"rank-math-link\">pip install<\/a> it!<\/p>\n\n\n\n<p>To import this module, simply use the below statement:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport http.server\n<\/pre><\/div>\n\n\n<p>Now you&#8217;re all set to run the server. Let&#8217;s now write a bit of code to serve the files.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Running our Http Server<\/h2>\n\n\n\n<p>If you simply want to share your files and directories to another user, you can directly run the server using Python.<\/p>\n\n\n\n<p>Go to whatever directory you wish to share, and run the server from there, using:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; title: ; notranslate\" title=\"\">\npython -m http.server 9000\n<\/pre><\/div>\n\n\n<p>Here, we start our local Http Server at port 9000.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Connecting to the Http Server<\/h2>\n\n\n\n<p>Now, to connect to the local server, you must do the following steps:<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>Go to the server machine, and find out the server IP Address using <code>arp -a<\/code> on Windows or <code>ip -a | grep inet<\/code> on Linux.<\/li><li>From the remote client, simply type <code>http:\/\/IP_ADDRESS:9000\/<\/code> on your browser and see the magic!<\/li><\/ol>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"426\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/06\/http_server_basic-1024x426.png\" alt=\"Http Server Basic\" class=\"wp-image-6339\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/06\/http_server_basic-1024x426.png 1024w, https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/06\/http_server_basic-300x125.png 300w, https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/06\/http_server_basic-768x320.png 768w, https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/06\/http_server_basic.png 1064w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption>Http Server Basic<\/figcaption><\/figure><\/div>\n\n\n\n<p>Note that you can look at the server files, or even download it to the client machine!<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"999\" height=\"440\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/06\/python_http_server_file.png\" alt=\"Python Http Server File\" class=\"wp-image-6340\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/06\/python_http_server_file.png 999w, https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/06\/python_http_server_file-300x132.png 300w, https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/06\/python_http_server_file-768x338.png 768w\" sizes=\"auto, (max-width: 999px) 100vw, 999px\" \/><figcaption>Python Http Server File<\/figcaption><\/figure><\/div>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Running a Python HttpServer that serves a custom index.html file<\/h2>\n\n\n\n<p>While the default server is a convenience for directly sharing files, you can customize the behavior of the server, by running a separate file.<\/p>\n\n\n\n<p>For example, we&#8217;ll be running a custom Http Server which uses <code>http.server<\/code> and <code>socketserver<\/code> for TCP Communication. <\/p>\n\n\n\n<p>The MyHttpRequestHandler calls do_GET() method to serve the request. To serve a custom file for the request, we can <a href=\"https:\/\/www.askpython.com\/python\/oops\/polymorphism-in-python\" class=\"rank-math-link\">override the function<\/a> by simply defining another do_GET() method that returns a different value. <\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n# server.py\nimport http.server # Our http server handler for http requests\nimport socketserver # Establish the TCP Socket connections\n\nPORT = 9000\n\nclass MyHttpRequestHandler(http.server.SimpleHTTPRequestHandler):\n    def do_GET(self):\n        self.path = &#039;index.html&#039;\n        return http.server.SimpleHTTPRequestHandler.do_GET(self)\n\nHandler = MyHttpRequestHandler\n\nwith socketserver.TCPServer((&quot;&quot;, PORT), Handler) as httpd:\n    print(&quot;Http Server Serving at port&quot;, PORT)\n    httpd.serve_forever()\n\n<\/pre><\/div>\n\n\n<p>If you call this as <code>server.py<\/code>, you can run the http server using:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\npython server.py\n<\/pre><\/div>\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"288\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/06\/index-html-1-1024x288.png\" alt=\"Custom homepage Python http server\" class=\"wp-image-6421\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/06\/index-html-1-1024x288.png 1024w, https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/06\/index-html-1-300x84.png 300w, https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/06\/index-html-1-768x216.png 768w, https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/06\/index-html-1.png 1421w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption>Custom Homepage for Python http server<\/figcaption><\/figure><\/div>\n\n\n\n<p>Because we defined our custom do_GET() function, we can serve a homepage HTML file using our server which is index.html in this case. Also if the server is running on your system, you can directly access the server using localhost:&lt;portnumber> instead of using the IP. <\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>In this article, we learned how to set up a simple Http server in Python, using the <code>http.server<\/code> module. We also learned how to define the do_GET() method to serve custom files when we make a request to our server.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">References<\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/docs.python.org\/3\/library\/http.server.html\" class=\"rank-math-link\" target=\"_blank\" rel=\"noopener\">Python Documentation<\/a> on Http Server<\/li><\/ul>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n","protected":false},"excerpt":{"rendered":"<p>Hello everyone! In today&#8217;s article, we&#8217;ll take a look at using Python HttpServer. This module serves as a very quick and easy way to start a local Http Server on your network. Earlier, in Python 2.7, this module was called HttpServer. But with Python3, this module has been merged into the http.server module. Let&#8217;s get [&hellip;]<\/p>\n","protected":false},"author":7,"featured_media":6342,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-6338","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python-modules"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/6338","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/users\/7"}],"replies":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/comments?post=6338"}],"version-history":[{"count":0,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/6338\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media\/6342"}],"wp:attachment":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media?parent=6338"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/categories?post=6338"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/tags?post=6338"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}