[Tut] Python One-Liner Webserver HTTP - Printable Version +- Sick Gaming (https://www.sickgaming.net) +-- Forum: Programming (https://www.sickgaming.net/forum-76.html) +--- Forum: Python (https://www.sickgaming.net/forum-83.html) +--- Thread: [Tut] Python One-Liner Webserver HTTP (/thread-95109.html) |
[Tut] Python One-Liner Webserver HTTP - xSicKxBot - 05-18-2020 Python One-Liner Webserver HTTP <div><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> <figure class="wp-block-image size-large"><img 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> <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> <p>The terminal will tell you:</p> <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> <p>To shut down your webserver, kill the Python program with <code>CTRL+c</code>.</p> <p>This works if you’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> <p><em>You can run this command in your Windows Powershell, Win Command Line, MacOS Terminal, or Linux Bash Script.</em></p> <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> <p><em><strong>Note</strong>: The IP address is <strong>NOT</strong> 0.0.0.0—this is an often-confused mistake by many readers. Instead, your webserver listens at your “local” 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> <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> <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 Serving HTTP on 0.0.0.0 port 8000 ...</pre> <p>If you want to start your webserver from within your Python script, no problem:</p> <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 import socketserver PORT = 8000 Handler = http.server.SimpleHTTPRequestHandler with socketserver.TCPServer(("", PORT), Handler) as httpd: print("serving at port", PORT) httpd.serve_forever()</pre> <p>You can execute this in our online Python browser (yes, you’re creating a local webserver in the browser—how cool is that)!</p> <figure><iframe src="https://repl.it/@finxter/webserver?lite=true" allowfullscreen="true" width="100%" height="1000px"></iframe></figure> <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>—feel free to read more if you’re interested in setting up the server (most of the code is relatively self-explanatory). </p> <h2>Where to Go From Here?</h2> <p>Enough theory, let’s get some practice!</p> <p>To become successful in coding, you need to get out there and solve real problems for real people. That’s how you can become a six-figure earner easily. And that’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs?</p> <p><strong>Practice projects is how you sharpen your saw in coding!</strong></p> <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> <p>Then become a Python freelance developer! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner.</p> <p>Join my free webinar <a rel="noreferrer noopener" href="https://blog.finxter.com/webinar-freelancer/" target="_blank">“How to Build Your High-Income Skill Python”</a> and watch how I grew my coding business online and how you can, too—from the comfort of your own home.</p> <p><a href="https://blog.finxter.com/webinar-freelancer/" target="_blank" rel="noreferrer noopener">Join the free webinar now!</a></p> </div> https://www.sickgaming.net/blog/2020/05/17/python-one-liner-webserver-http/ |