-
-
Save CSonTi04/ce21e8a5398cb23ac104d3d2540f08ed to your computer and use it in GitHub Desktop.
Revisions
-
nitaku revised this gist
May 28, 2015 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
LoadingSorry, something went wrong. Reload?Sorry, we cannot display this file.Sorry, this file is invalid so it cannot be displayed. -
nitaku revised this gist
May 27, 2015 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
LoadingSorry, something went wrong. Reload?Sorry, we cannot display this file.Sorry, this file is invalid so it cannot be displayed. -
nitaku revised this gist
Jun 23, 2014 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 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: -
nitaku revised this gist
Jun 23, 2014 . 1 changed file with 2 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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: ``` -
nitaku revised this gist
Jun 23, 2014 . 1 changed file with 24 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1 +1,24 @@ 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 ``` -
nitaku revised this gist
Jun 23, 2014 . 3 changed files with 71 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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()
LoadingSorry, something went wrong. Reload?Sorry, we cannot display this file.Sorry, this file is invalid so it cannot be displayed. -
nitaku revised this gist
Jun 23, 2014 . 1 changed file with 7 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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/). 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 ``` -
nitaku revised this gist
Jun 23, 2014 . 1 changed file with 14 additions and 8 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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"} ``` 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/). -
nitaku revised this gist
Jun 23, 2014 . 1 changed file with 2 additions and 4 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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"} -
nitaku revised this gist
Jun 23, 2014 . 1 changed file with 2 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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"} -
nitaku revised this gist
Jun 23, 2014 . 1 changed file with 9 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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/). -
nitaku revised this gist
Jun 23, 2014 . 1 changed file with 3 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1 +1,3 @@ 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/). -
nitaku created this gist
Jun 23, 2014 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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).