{"id":113036,"date":"2020-05-17T14:18:19","date_gmt":"2020-05-17T14:18:19","guid":{"rendered":"https:\/\/blog.finxter.com\/?p=8635"},"modified":"2020-05-17T14:18:19","modified_gmt":"2020-05-17T14:18:19","slug":"python-one-liner-webserver-http","status":"publish","type":"post","link":"https:\/\/sickgaming.net\/blog\/2020\/05\/17\/python-one-liner-webserver-http\/","title":{"rendered":"Python One-Liner Webserver HTTP"},"content":{"rendered":"<p>Want to create your own webserver in a <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/python-one-liners-the-ultimate-collection\/\" target=\"_blank\">single line of Python code<\/a>? No problem, just use this command in your shell:<\/p>\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" src=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2020\/05\/image-61.png\" alt=\"\" class=\"wp-image-8636\" srcset=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2020\/05\/image-61.png 743w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2020\/05\/image-61-300x124.png 300w\" sizes=\"(max-width: 743px) 100vw, 743px\" \/><\/figure>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"shell\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">$ python -m http.server 8000<\/pre>\n<p>The terminal will tell you:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"shell\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">Serving HTTP on 0.0.0.0 port 8000<\/pre>\n<p>To shut down your webserver, kill the Python program with <code>CTRL+c<\/code>.<\/p>\n<p>This works if you&#8217;ve <strong>Python 3<\/strong> installed on your system. To check your version, use the command <code><a href=\"https:\/\/blog.finxter.com\/how-to-check-your-python-version\/\">python --version<\/a><\/code> in your shell. <\/p>\n<p><em>You can run this command in your Windows Powershell, Win Command Line, MacOS Terminal, or Linux Bash Script.<\/em><\/p>\n<p>You can see in the screenshot that the server runs on your local host listening on port 8000 (the standard HTTP port to serve web requests).<\/p>\n<p><em><strong>Note<\/strong>: The IP address is <strong>NOT<\/strong> 0.0.0.0&#8212;this is an often-confused mistake by many readers. Instead, your webserver listens at your &#8220;local&#8221; IP address 127.0.0.1 on port 8000. Thus, only web requests issued on your computer will arrive at this port. The webserver is NOT visible to the outside world.<\/em><\/p>\n<p><strong>Python 2<\/strong>: To run the same simple webserver on Python 2, you need to use another command using <a href=\"https:\/\/docs.python.org\/2\/library\/simplehttpserver.html\" target=\"_blank\" rel=\"noreferrer noopener\"><code>SimpleHTTPServer<\/code> <\/a>instead of <code>http<\/code>:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"shell\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">$ python -m SimpleHTTPServer 8000\nServing HTTP on 0.0.0.0 port 8000 ...<\/pre>\n<p>If you want to start your webserver from within your Python script, no problem:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import http.server\nimport socketserver PORT = 8000 Handler = http.server.SimpleHTTPRequestHandler with socketserver.TCPServer((\"\", PORT), Handler) as httpd: print(\"serving at port\", PORT) httpd.serve_forever()<\/pre>\n<p>You can execute this in our online Python browser (yes, you&#8217;re creating a local webserver in the browser&#8212;how cool is that)!<\/p>\n<figure><iframe loading=\"lazy\" src=\"https:\/\/repl.it\/@finxter\/webserver?lite=true\" allowfullscreen=\"true\" width=\"100%\" height=\"1000px\"><\/iframe><\/figure>\n<p>This code comes from the <a rel=\"noreferrer noopener\" href=\"https:\/\/docs.python.org\/3\/library\/http.server.html\" target=\"_blank\">official Python documentation<\/a>&#8212;feel free to read more if you&#8217;re interested in setting up the server (most of the code is relatively self-explanatory). <\/p>\n<h2>Where to Go From Here?<\/h2>\n<p>Enough theory, let\u2019s get some practice!<\/p>\n<p>To become successful in coding, you need to get out there and solve real problems for real people. That\u2019s how you can become a six-figure earner easily. And that\u2019s how you polish the skills you really need in practice. After all, what\u2019s the use of learning theory that nobody ever needs?<\/p>\n<p><strong>Practice projects is how you sharpen your saw in coding!<\/strong><\/p>\n<p>Do you want to become a code master by focusing on practical code projects that actually earn you money and solve problems for people?<\/p>\n<p>Then become a Python freelance developer! It\u2019s the best way of approaching the task of improving your Python skills\u2014even if you are a complete beginner.<\/p>\n<p>Join my free webinar <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/webinar-freelancer\/\" target=\"_blank\">\u201cHow to Build Your High-Income Skill Python\u201d<\/a> and watch how I grew my coding business online and how you can, too\u2014from the comfort of your own home.<\/p>\n<p><a href=\"https:\/\/blog.finxter.com\/webinar-freelancer\/\" target=\"_blank\" rel=\"noreferrer noopener\">Join the free webinar now!<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Want to create your own webserver in a single line of Python code? No problem, just use this command in your shell: $ python -m http.server 8000 The terminal will tell you: Serving HTTP on 0.0.0.0 port 8000 To shut down your webserver, kill the Python program with CTRL+c. This works if you&#8217;ve Python 3 [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[857],"tags":[73,468,528],"class_list":["post-113036","post","type-post","status-publish","format-standard","hentry","category-python-tut","tag-programming","tag-python","tag-tutorial"],"_links":{"self":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/113036","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/comments?post=113036"}],"version-history":[{"count":0,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/113036\/revisions"}],"wp:attachment":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/media?parent=113036"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/categories?post=113036"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/tags?post=113036"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}