-
-
Save ericoporto/9389dbd99aae57f85460 to your computer and use it in GitHub Desktop.
A minimal http server and client pair. It's intended to show how to communicate between a server and a client in Python.
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 characters
#!/usr/bin/env python | |
""" | |
Very simple HTTP server and client pair in python. | |
Server based on: https://gist.github.com/bradmontgomery/2219997 | |
Usage:: | |
./server-http.py start [<port>] | |
Send a message (on a second terminal) :: | |
./server-http.py notify "hello world!" | |
Stop the server (on a second terminal) :: | |
./server-http.py stop | |
""" | |
from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer | |
import SocketServer | |
import sys, urllib2, json | |
def keep_running(): | |
global keeprun | |
return keeprun | |
def stop_running(): | |
global keeprun | |
keeprun = False | |
def start_running(): | |
global keeprun | |
keeprun = True | |
class S(BaseHTTPRequestHandler): | |
def _set_headers(self): | |
self.send_response(200) | |
self.send_header('Content-type', 'text/html') | |
self.end_headers() | |
def do_POST(self): | |
content_len = int(self.headers.getheader('content-length', 0)) | |
post_body = self.rfile.read(content_len) | |
teststop = post_body.split() | |
self._set_headers() | |
print(post_body) | |
if(teststop[0]=='stop'): | |
stop_running() | |
def run(server_class=HTTPServer, handler_class=S, port=2048): | |
server_address = ('', port) | |
httpd = server_class(server_address, handler_class) | |
print 'Starting httpd...' | |
start_running() | |
while keep_running(): | |
httpd.handle_request() | |
def ask(message): | |
data = {'text': message} | |
json_data = json.dumps(data) | |
req = urllib2.Request('http://localhost:2048') | |
result = urllib2.urlopen(req, json_data) | |
def askstop(): | |
req = urllib2.Request('http://localhost:2048') | |
result = urllib2.urlopen(req, 'stop') | |
def showusage(): | |
print "usage: %s start <port> | stop | notify \"<notification>\"" % sys.argv[0] | |
sys.exit(2) | |
def unknowncommand(): | |
print "Unknown command" | |
sys.exit(2) | |
sys.exit(0) | |
if __name__ == "__main__": | |
from sys import argv | |
if len(argv) == 2: | |
if(argv[1]=='start'): | |
run() | |
elif(argv[1]=='stop'): | |
askstop() | |
elif(argv[1]=='--help'): | |
showusage() | |
else: | |
unknowncommand() | |
elif len(argv) == 3: | |
if(argv[1]=='start'): | |
run(port=int(argv[2])) | |
elif(argv[1]=='notify'): | |
ask(argv[2]) | |
else: | |
unknowncommand() | |
else: | |
showusage() |
@MxShift I have some Python3 code in two repos that may have related code
https://github.com/ericoporto/hotreload
https://github.com/ericoporto/fgmk/blob/master/fgmk/game_server.py
I may need to update the above code if it's being found in Google or something.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Cool, do you have a version for Python 3?