1
0
Fork 0

warning, buggy version!

- class file folder is include/
- moved content handler to an individual class file (include/content.py)
- added argument parser (include/argparser.py) -- doesn't work yet like expected
This commit is contained in:
fanir 2013-04-24 20:25:50 +02:00
parent ee7ffadfbd
commit daa40d6367
5 changed files with 255 additions and 65 deletions

2
include/__init__.py Normal file
View file

@ -0,0 +1,2 @@
from .argparser import argparser
from .content import content

78
include/argparser.py Normal file
View file

@ -0,0 +1,78 @@
# includes/argparser.py
#
# module version: 1.0.20130424
#
class argparser:
def parse(program_version, protocol_version):
import argparse
parser = argparse.ArgumentParser(
description = "The server side of the "\
"aliased server remote control",
add_help = False)
parser.add_argument(
"--help",
action = "help",
help = "show this help message and exit")
parser.add_argument(
"--version",
action = "version",
version = "Server version: " + program_version +\
" / Protocol version: " + protocol_version)
grp_pid_file_failure = parser.add_mutually_exclusive_group()
grp_pid_file_failure.add_argument(
"-n",
"--delete-pid-file",
action = "store_true",
help = "deletes the pid file and starts the server")
grp_pid_file_failure.add_argument(
"-m",
"--allow-multiple-instances",
action = "store_true",
help = "ignores the pid file and starts the server. "\
"Will not create another pid file.")
parser.add_argument(
"-v",
"--verbosity",
type = int,
choices = range(0, 4),
# default = -1,
help = "increase output verbosity. Can be from 0 "\
"(only default output) to 3 (debug messages).")
parser.add_argument(
"-h",
"--host",
# default = -1,
help = "IP or hostname (use 0.0.0.0 for all interfaces) on which "\
"the server should listen")
parser.add_argument(
"-p",
"--port",
# default = -1,
help = "the port on which the server should listen")
parser.add_argument(
"-t",
"--timeout",
# default = -1,
help = "timeout of a connection in seconds - still "\
"doesn't work obviously...")
parser.add_argument(
"-e",
"--encoding",
# default = -1,
help = "encoding to be used when communicating with clients")
return parser.parse_args()

78
include/content.py Normal file
View file

@ -0,0 +1,78 @@
# includes/content.py
#
# module version: 0.0.20130424
#
class content:
# handler the content
def handler(client_address, data, aliases, server_version, protocol_version, verbosity):
ret = ""
ret = ret +\
"{BEGIN}\n"\
"asrcp" + protocol_version + "\n"
# Look if the received message is an
# valid alias or a predefined command
# if it's 'version', return the server and protocol version
if data == "version":
if verbosity >= 2: print("Got valid service command from"
+ str(client_address) + ": ", data)
ret = ret +\
"202 Valid Service Command\n"\
"002 Version\n"\
"ServerVersion:" + server_version + "\n"\
"ProtocolVersion:" + protocol_version + "\n"
# if it's 'help', give a little help
elif data == 'help':
if verbosity >= 2: print("Got valid command from"
+ str(client_address) + ": ", data)
# send status code
ret = ret + "202 Valid Service Command\n\n"
# send the list of aliases
ret = ret + "Aviable aliases:\n"
for i in aliases.keys():
ret = ret + str(i) + "\n"
# if it's a valid userdefined command
elif data in aliases:
# send status code
ret = ret + "201 Valid Command\n\n"
# ohmagawd! a debug message!!1!
if verbosity >= 2: print("Got valid command from"
+ str(client_address) + ": ", data)
# execute the aliased command
g_dict, l_dict = {}, {}
exec(str(aliases[data]), g_dict, l_dict)
# send may contain data to send to the client
if l_dict["send"]:
content = str(l_dict["send"]).replace('{', '\{')
content = content.replace('}', '\}')
ret = ret + content + "\n"
# ALL IS LOST!!1! this has to be invalid!
else:
# send status code
ret = ret + "203 Invalid Command\n"
if verbosity >= 2: print("Got invalid command from",
str(client_address), ": ", data)
ret = ret + "{END}\n"
return ret