Skip to content

Instantly share code, notes, and snippets.

@CSonTi04
Forked from nitaku/README.md
Created March 3, 2024 20:41
Show Gist options
  • Select an option

  • Save CSonTi04/ce21e8a5398cb23ac104d3d2540f08ed to your computer and use it in GitHub Desktop.

Select an option

Save CSonTi04/ce21e8a5398cb23ac104d3d2540f08ed to your computer and use it in GitHub Desktop.

Revisions

  1. @nitaku nitaku revised this gist May 28, 2015. 1 changed file with 0 additions and 0 deletions.
    Binary file modified thumbnail.png
    Loading
    Sorry, something went wrong. Reload?
    Sorry, we cannot display this file.
    Sorry, this file is invalid so it cannot be displayed.
  2. @nitaku nitaku revised this gist May 27, 2015. 1 changed file with 0 additions and 0 deletions.
    Binary file modified thumbnail.png
    Loading
    Sorry, something went wrong. Reload?
    Sorry, we cannot display this file.
    Sorry, this file is invalid so it cannot be displayed.
  3. @nitaku nitaku revised this gist Jun 23, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion README.md
    Original file line number Diff line number Diff line change
    @@ -17,7 +17,7 @@ curl --data "{\"this\":\"is a test\"}" --header "Content-Type: application/json"

    Adapted from [this Gist](https://gist.github.com/bradmontgomery/2219997), with the addition of code for reading the request body taken from [this article](http://mafayyaz.wordpress.com/2013/02/08/writing-simple-http-server-in-python-with-rest-and-json/).

    Please be careful when using a server like this on production environments, because it lacks many important features (threading to name one). You can consult [the python documentation about BaseHTTPServer](https://docs.python.org/2/library/basehttpserver.html) to improve it.
    Please be careful when using a server like this on production environments, because it lacks many important features (threading to name one). You can consult [the python documentation about BaseHTTPServer](https://docs.python.org/2/library/basehttpserver.html) to learn something useful to improve it.

    If you are on Ubuntu, you can install this code as a service with an init script (hopefully, with some modifications that make it actually do something useful). Just modify the include `server.conf` to suit your needs (possibly renaming it and redirecting output to some log files instead of `/dev/null`), and copy it into `/etc/init/`. You can then start/stop/restart the server with the usual `service` command:

  4. @nitaku nitaku revised this gist Jun 23, 2014. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -17,6 +17,8 @@ curl --data "{\"this\":\"is a test\"}" --header "Content-Type: application/json"

    Adapted from [this Gist](https://gist.github.com/bradmontgomery/2219997), with the addition of code for reading the request body taken from [this article](http://mafayyaz.wordpress.com/2013/02/08/writing-simple-http-server-in-python-with-rest-and-json/).

    Please be careful when using a server like this on production environments, because it lacks many important features (threading to name one). You can consult [the python documentation about BaseHTTPServer](https://docs.python.org/2/library/basehttpserver.html) to improve it.

    If you are on Ubuntu, you can install this code as a service with an init script (hopefully, with some modifications that make it actually do something useful). Just modify the include `server.conf` to suit your needs (possibly renaming it and redirecting output to some log files instead of `/dev/null`), and copy it into `/etc/init/`. You can then start/stop/restart the server with the usual `service` command:

    ```
  5. @nitaku nitaku revised this gist Jun 23, 2014. 1 changed file with 24 additions and 1 deletion.
    25 changes: 24 additions & 1 deletion README.md
    Original file line number Diff line number Diff line change
    @@ -1 +1,24 @@
    A minimal HTTP server in python. Adapted from [this Gist](https://gist.github.com/bradmontgomery/2219997).
    A minimal HTTP server in python. It sends a JSON Hello World for GET requests, and echoes back JSON for POST requests.

    ```
    python server.py 8009
    Starting httpd on port 8009...
    ```

    ```
    curl http://localhost:8009
    {"received": "ok", "hello": "world"}
    ```

    ```
    curl --data "{\"this\":\"is a test\"}" --header "Content-Type: application/json" http://localhost:8009
    {"this": "is a test", "received": "ok"}
    ```

    Adapted from [this Gist](https://gist.github.com/bradmontgomery/2219997), with the addition of code for reading the request body taken from [this article](http://mafayyaz.wordpress.com/2013/02/08/writing-simple-http-server-in-python-with-rest-and-json/).

    If you are on Ubuntu, you can install this code as a service with an init script (hopefully, with some modifications that make it actually do something useful). Just modify the include `server.conf` to suit your needs (possibly renaming it and redirecting output to some log files instead of `/dev/null`), and copy it into `/etc/init/`. You can then start/stop/restart the server with the usual `service` command:

    ```
    sudo service server start
    ```
  6. @nitaku nitaku revised this gist Jun 23, 2014. 3 changed files with 71 additions and 0 deletions.
    16 changes: 16 additions & 0 deletions server.conf
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,16 @@
    description "Example JSON HTTP server"
    author "nitaku - [email protected]"

    start on started mountall
    stop on shutdown

    respawn
    respawn limit 99 5

    script
    exec sudo -u www-data /usr/bin/python /data/examples/python_minimal_http/server.py 8009 >> /dev/null 2>> /dev/null

    end script

    post-start script
    end script
    55 changes: 55 additions & 0 deletions server.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,55 @@
    from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
    import SocketServer
    import json
    import cgi

    class Server(BaseHTTPRequestHandler):
    def _set_headers(self):
    self.send_response(200)
    self.send_header('Content-type', 'application/json')
    self.end_headers()

    def do_HEAD(self):
    self._set_headers()

    # GET sends back a Hello world message
    def do_GET(self):
    self._set_headers()
    self.wfile.write(json.dumps({'hello': 'world', 'received': 'ok'}))

    # POST echoes the message adding a JSON field
    def do_POST(self):
    ctype, pdict = cgi.parse_header(self.headers.getheader('content-type'))

    # refuse to receive non-json content
    if ctype != 'application/json':
    self.send_response(400)
    self.end_headers()
    return

    # read the message and convert it into a python dictionary
    length = int(self.headers.getheader('content-length'))
    message = json.loads(self.rfile.read(length))

    # add a property to the object, just to mess with data
    message['received'] = 'ok'

    # send the message back
    self._set_headers()
    self.wfile.write(json.dumps(message))

    def run(server_class=HTTPServer, handler_class=Server, port=8008):
    server_address = ('', port)
    httpd = server_class(server_address, handler_class)

    print 'Starting httpd on port %d...' % port
    httpd.serve_forever()

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

    if len(argv) == 2:
    run(port=int(argv[1]))
    else:
    run()

    Binary file added thumbnail.png
    Loading
    Sorry, something went wrong. Reload?
    Sorry, we cannot display this file.
    Sorry, this file is invalid so it cannot be displayed.
  7. @nitaku nitaku revised this gist Jun 23, 2014. 1 changed file with 7 additions and 1 deletion.
    8 changes: 7 additions & 1 deletion README.md
    Original file line number Diff line number Diff line change
    @@ -15,4 +15,10 @@ curl --data "{\"this\":\"is a test\"}" --header "Content-Type: application/json"
    {"this": "is a test", "received": "ok"}
    ```

    Adapted from [this Gist](https://gist.github.com/bradmontgomery/2219997), with the addition of code for reading the request body taken from [this article](http://mafayyaz.wordpress.com/2013/02/08/writing-simple-http-server-in-python-with-rest-and-json/).
    Adapted from [this Gist](https://gist.github.com/bradmontgomery/2219997), with the addition of code for reading the request body taken from [this article](http://mafayyaz.wordpress.com/2013/02/08/writing-simple-http-server-in-python-with-rest-and-json/).

    If you are on Ubuntu, you can install this code as a service with an init script (hopefully, with some modifications that make it actually do something useful). Just modify the include `server.conf` to suit your needs (possibly renaming it and redirecting output to some log files instead of `/dev/null`), and copy it into `/etc/init/`. You can then start/stop/restart the server with the usual `service` command:

    ```
    sudo service server start
    ```
  8. @nitaku nitaku revised this gist Jun 23, 2014. 1 changed file with 14 additions and 8 deletions.
    22 changes: 14 additions & 8 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -1,12 +1,18 @@
    A minimal HTTP server in python. It sends a JSON Hello World for GET requests, and echoes back JSON for POST requests.

    > python server.py 8009
    > Starting httpd on port 8009...
    >
    > curl http://localhost:8009
    > {"received": "ok", "hello": "world"}
    >
    > curl --data "{\"this\":\"is a test\"}" --header "Content-Type: application/json" http://localhost:8009
    > {"this": "is a test", "received": "ok"}
    ```
    python server.py 8009
    Starting httpd on port 8009...
    ```

    ```
    curl http://localhost:8009
    {"received": "ok", "hello": "world"}
    ```

    ```
    curl --data "{\"this\":\"is a test\"}" --header "Content-Type: application/json" http://localhost:8009
    {"this": "is a test", "received": "ok"}
    ```

    Adapted from [this Gist](https://gist.github.com/bradmontgomery/2219997), with the addition of code for reading the request body taken from [this article](http://mafayyaz.wordpress.com/2013/02/08/writing-simple-http-server-in-python-with-rest-and-json/).
  9. @nitaku nitaku revised this gist Jun 23, 2014. 1 changed file with 2 additions and 4 deletions.
    6 changes: 2 additions & 4 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -2,12 +2,10 @@ A minimal HTTP server in python. It sends a JSON Hello World for GET requests, a

    > python server.py 8009
    > Starting httpd on port 8009...

    >
    > curl http://localhost:8009
    > {"received": "ok", "hello": "world"}

    >
    > curl --data "{\"this\":\"is a test\"}" --header "Content-Type: application/json" http://localhost:8009
    > {"this": "is a test", "received": "ok"}
  10. @nitaku nitaku revised this gist Jun 23, 2014. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -3,9 +3,11 @@ A minimal HTTP server in python. It sends a JSON Hello World for GET requests, a
    > python server.py 8009
    > Starting httpd on port 8009...

    > curl http://localhost:8009
    > {"received": "ok", "hello": "world"}

    > curl --data "{\"this\":\"is a test\"}" --header "Content-Type: application/json" http://localhost:8009
    > {"this": "is a test", "received": "ok"}
  11. @nitaku nitaku revised this gist Jun 23, 2014. 1 changed file with 9 additions and 0 deletions.
    9 changes: 9 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,12 @@
    A minimal HTTP server in python. It sends a JSON Hello World for GET requests, and echoes back JSON for POST requests.

    > python server.py 8009
    > Starting httpd on port 8009...
    > curl http://localhost:8009
    > {"received": "ok", "hello": "world"}
    > curl --data "{\"this\":\"is a test\"}" --header "Content-Type: application/json" http://localhost:8009
    > {"this": "is a test", "received": "ok"}
    Adapted from [this Gist](https://gist.github.com/bradmontgomery/2219997), with the addition of code for reading the request body taken from [this article](http://mafayyaz.wordpress.com/2013/02/08/writing-simple-http-server-in-python-with-rest-and-json/).
  12. @nitaku nitaku revised this gist Jun 23, 2014. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion README.md
    Original file line number Diff line number Diff line change
    @@ -1 +1,3 @@
    A minimal HTTP server in python. Adapted from [this Gist](https://gist.github.com/bradmontgomery/2219997).
    A minimal HTTP server in python. It sends a JSON Hello World for GET requests, and echoes back JSON for POST requests.

    Adapted from [this Gist](https://gist.github.com/bradmontgomery/2219997), with the addition of code for reading the request body taken from [this article](http://mafayyaz.wordpress.com/2013/02/08/writing-simple-http-server-in-python-with-rest-and-json/).
  13. @nitaku nitaku created this gist Jun 23, 2014.
    1 change: 1 addition & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    A minimal HTTP server in python. Adapted from [this Gist](https://gist.github.com/bradmontgomery/2219997).