Last active
August 29, 2015 14:11
-
-
Save atiaxi/cbbef0ef5545744b147f to your computer and use it in GitHub Desktop.
This file contains 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 | |
# -*- coding: utf-8 -*- | |
#### PEP8: Module names are usually lowercase; something like `chromabot_listener.py` | |
""" | |
The listening server for the Chromeconomist's connection with Chromabot. | |
Very, very sketchy and impromptu, my first try at something even remotely similar to a | |
REST-like API. Hopefully the pseudo-API I provide Reostra with, in addition to this listening | |
server, will allow Chromabot and Chromeconomist to mesh, adding new depth to the game. | |
Thanks go to Daniel Zappala at BYU for the skeleton of the listening code, found here: | |
http://ilab.cs.byu.edu/python/socket/echoserver.html | |
""" | |
import socket | |
import json | |
import sys | |
host = '' | |
port = 17236 | |
backlog = 5 | |
size = 1024 | |
#### Hardcoding these values probably works short-term, but longer term they | |
#### could be rolled into the class/function call (see below) | |
s = socket.socket(socket.AF_INET,socket.SOCK_STREAM) | |
s.bind((host,port)) | |
s.listen(backlog) | |
#### You probably don't want to do this at the module level; this will establish a | |
#### connection any time someone does a `import ChromabotListener`. Mostly you want | |
#### to do things like start up a connection explicitly on a function call. | |
def readTerritory(territory): | |
territory = str(territory) | |
territory = territory.replace('-','') | |
territory = territory.replace('_','') | |
territory = territory.lower() | |
return territory | |
def parseRequest(request): | |
"""parse the request Chromabot sends, and return the proper buff as a float""" | |
#### It'd be useful to have some documentation of what `request` should look like. Judging | |
#### from the code below, it looks like it's just the raw text of a territory name, so I'm | |
#### going to continue review with that assumption in mind | |
#convert the territory Reo sends into one guaranteed to be readable | |
territory = readTerritory(request) | |
#make sure that the json file works, first. If it doesn't, return an error to Chromabot instead | |
#the API will take care of errors with Reo needing to code anything else. | |
try: | |
chromaData = json.load(open("EconomyInfo.json",'r')) | |
#### You open this file but never close it. This is also a good spot to use a 'with' statement: | |
#### chromaData = None | |
#### with open('EconomyInfo.json','r') as f: | |
#### chromaData = json.load(f) | |
except: | |
#### It's generally bad form to do unqualified 'except', as this catches *everything* | |
#### including things you probably don't want to catch (e.g. KeyboardInterrupt) | |
e = sys.exc_info() | |
return ('ERROR:'+str(e)) | |
#check to see if the request is a valid one | |
if territory in chromaData['LandInfo'].keys(): | |
return request + str(chromaData['LandInfo'][territory]['DEFbuff']) | |
#### The docstring says that this function returns a float, but it looks from this | |
#### line like it's going to return a string of the form "region0.1", which is | |
#### going to be... interesting to try to parse on the client end :) | |
else: | |
return "ERROR:NotTerritory" | |
while True: | |
#### Similar to above, you don't want to put your listen loop at module-level, which | |
#### would mean any time anyone imports this module, it'll block and start listening. | |
#### If this script is intended to be run, wrap this portion in a | |
#### `if __name__ == '__main__':` block | |
try: | |
client, address = s.accept() | |
request = client.recv(size) | |
if request: | |
client.send(parseRequest(request)) | |
client.close() | |
except KeyboardInterrupt: | |
#### Is there any reason to catch KeyboardInterrupt here? By default it'll stop | |
#### the program so I'm not sure you're gaining anything. | |
print "Exiting program!" | |
break | |
except: | |
pass | |
#### This `except` statement seems to be superfluous |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment