How to deploy a Python http server that I can make requests to from my local machine

Hi

I am new to Linode. I have not set up anything yet. I am doing my due diligence on this.

My situation is this: I have a small Python Http Server whose code is pasted below.

I am able to send GET or post requests to this server locally, get the appropriate responses.

It is available as: http://localhost:8081

Now, my task is to host the same http server here on Linode.

That said, I would like to be able to issue the same requests to this python server from my local machine command line, and get the appropriate responses.

For example: curl http://mylinodeserver:<> for a GET

and curl -d "foo=bar&bin=baz" http://mylinodeserver

What do I need to get this accomplished on Linode? Thanks for all the help.

#!/usr/bin/env python
"""
Very simple HTTP server in python.

Usage:: < <assign 8081="" a="" random="" value="" of="" t0="" <port="">./dummy-web-server.py [<port>]

Send a GET request::
    curl http://localhost: <port>Send a HEAD request::
    curl -I http://localhost

Send a POST request::
    curl -d "foo=bar&bin=baz" http://localhost

"""
from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
import SocketServer

import serial #Serial imported for Serial communication
import time #Required to use delay functions
import sys
from urllib2 import *
import json
import urllib

class S(BaseHTTPRequestHandler):
    def _set_headers(self):
        self.send_response(200)
        self.send_header('Content-type', 'text/html')
        self.end_headers()

    def do_GET(self):
        self._set_headers()
        self.wfile.write("

# Hello from Save Me Chip!

")

    def do_HEAD(self):
        self._set_headers()

    def do_POST(self):
        # Doesn't do anything with posted data
        print("Entered do_POST!")
        self._set_headers()
        self.wfile.write("

# POST!

")

def run(server_class=HTTPServer, handler_class=S, port=8081):
    server_address = ('', port)
    httpd = server_class(server_address, handler_class)
    print 'Starting httpd...'
    httpd.serve_forever()

if __name__ == "__main__":
    from sys import argv

    if len(argv) == 2:
        run(port=int(argv[1]))
    else:
        run()</port></port></assign> 

1 Reply

server_address = ('', port)

This line tells the server which IP and port to bind to. Since the address part is empty, it should bind to all available interfaces and ports. So you should just be able to launch it on your linode and connect to it with http://1.2.3.4:8081 (where 1.2.3.4 is your Linode IP and 8081 is the default port). However I would strongly caution you against running unprotected custom code on a public Internet server.

Reply

Please enter an answer
Tips:

You can mention users to notify them: @username

You can use Markdown to format your question. For more examples see the Markdown Cheatsheet.

> I’m a blockquote.

I’m a blockquote.

[I'm a link] (https://www.google.com)

I'm a link

**I am bold** I am bold

*I am italicized* I am italicized

Community Code of Conduct