1
0
Fork 0

- cleaned class argparser

- commented and cleaned asrc-server.py
- renamed includes/content.py to includes/comm.py
- renamed class content to comm
- made some vars (aliases, server_version, protocol_version, verbiosity) global in class comm
- added init() to class comm for setting lobal vars
- moved motd() from asrc-server-py to class comm
- conformed comm.motd() to reference
- added comm.header() for building the headers
- added includes/statuscodes.py with class statuscodes with dict description for storing the status codes and their description
- added docstrings
This commit is contained in:
fanir 2013-04-26 12:55:37 +02:00
parent fbb4653740
commit 1207c6b225
6 changed files with 306 additions and 297 deletions

View file

@ -25,7 +25,7 @@
# aSRC (Aliased Server Remote Control)
# - SERVER -
#
# program version: 0.0.0.20130424
# program version: 0.0.0.20130426
# protocol version: 0.2.20130423
#
#
@ -35,84 +35,12 @@
import sys, os, socket, socketserver, threading, time
def motd():
return MOTD
#def content(client_address, data):
#ret = ""
#ret = ret +\
#"{BEGIN}\n"\
#"asrcp" + ProtocolVersion + "\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:" + ServerVersion + "\n"\
#"ProtocolVersion:" + ProtocolVersion + "\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
class ThreadedRequestHandler(socketserver.StreamRequestHandler):
def handle(self):
"""
Handles incoming connections
"""
# Set time for timeout in seconds
self.timeout = TIMEOUT
@ -121,20 +49,15 @@ class ThreadedRequestHandler(socketserver.StreamRequestHandler):
if VERBOSITY >=3:
print("Client connected: " + str(self.client_address))
# send header line 1
self.request.sendall(bytes
("asrpc " + ProtocolVersion + "\n", ENCODING))
# send motd
self.request.sendall(bytes(motd() + "\n", ENCODING))
self.request.sendall(comm.motd(MOTD) + "\n", ENCODING))
# Receive data
self.data = str(self.rfile.readline().strip(), ENCODING)
# content handler
self.request.sendall(bytes(
content.handler(str(self.client_address), self.data, aliases,
ServerVersion, ProtocolVersion, VERBOSITY), ENCODING))
comm.handler(str(self.client_address), self.data), ENCODING))
class ThreadedTCPServer(socketserver.ThreadingMixIn, socketserver.TCPServer):
@ -142,26 +65,33 @@ class ThreadedTCPServer(socketserver.ThreadingMixIn, socketserver.TCPServer):
def main():
# import global settings
global VERBOSITY, HOST, PORT, TIMEOUT, ENCODING
from include import argparser, content
from include import argparser, comm
comm.init(ServerVersion, ProtocolVersion, VERBOSITY, aliases)
# parse arguments
parser = argparser
args = parser.parse(ServerVersion, ProtocolVersion)
# look for pid file
if os.path.exists("pid"):
# remove, ...
if args.delete_pid_file: os.remove("pid")
# ... ignore ...
elif args.allow_multiple_instances: pass
# ... or exit
else:
print(
"\npid file already exists\n"\
"If the server didn't shut down correctly, just delete this file "\
"or pass -n\n"\
"If the server didn't shut down correctly, "\
"just delete this file or pass -n\n"\
"If you want to start multiple instances, pass -m\n")
return 1
print(args)
# set settings if command line options are given
if args.host: HOST = args.host
if args.port: PORT = args.port
if args.timeout: TIMEOUT = args.timeout
@ -193,9 +123,11 @@ def main():
ServerThread.daemon = True
ServerThread.start()
while True:
time.sleep(10)
# exit if Ctrl-C is pressed
except KeyboardInterrupt:
print("\nGot Ctrl-C, shutting down server...")

Binary file not shown.

View file

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

View file

@ -1,12 +1,16 @@
# includes/argparser.py
#
# module version: 1.0.20130424
# module version: 1.0.20130425
#
class argparser:
def parse(program_version, protocol_version):
"""
Parses comand line arguments
"""
import argparse
parser = argparse.ArgumentParser(
@ -45,34 +49,29 @@ class argparser:
"--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()

View file

@ -1,18 +1,59 @@
# includes/content.py
# includes/comm.py
#
# module version: 0.0.20130424
# module version: 0.0.20130426
#
class content:
import statuscodes
# handler the content
def handler(client_address, data, aliases, server_version, protocol_version, verbosity):
ret = ""
ret = ret +\
class comm:
aliases = dict()
server_version = ""
protocol_version = ""
verbosity = 0
# initializes global settings
def init(ServerVersion, ProtocolVersion, Verbosity, Aliases):
aliases = Aliases
server_version = ServerVersion
protocol_version = ProtocolVersion
verbosity = Verbosity
def header(statuscode, AdditionalHeaderLines = ""):
"""
returns the header
"""
ret =\
"{BEGIN}\n"\
"asrcp" + protocol_version + "\n"
"asrcp" + protocol_version + "\n"\
statuscode + " " + statuscodes.description[statuscodes] + "\n"\
"ServerVersion:" + server_version + "\n"
if AdditionalHeaderLines != "": ret += AdditionalHeaderLines + "\n"
ret += "\n\n"
return ret
# returns the motd
def motd(motd):
"""
builds and returns a motd package
"""
ret = motd
return ret
# handles the content
def command(client_address, data):
"""
processes a command
"""
ret = ""
# Look if the received message is an
# valid alias or a predefined command

37
include/statuscodes.py Normal file
View file

@ -0,0 +1,37 @@
class statuscodes:
description = dict(
# 000 - 400 server side
# 000 information
001 = "OK",
002 = "Version",
003 = "MOTD",
# 100 authentication and maintenance
101 = "Challenge",
102 = "Success",
103 = "Failure",
104 = "To Many Tries",
# 200 command
201 = "Valid",
202 = "Valid Service Command",
203 = "Invalid",
204 = "Failure",
205 = "Continue",
# 300 server
301 = "Unhandled Exception",
302 = "Shutting Down",
303 = "Restarting",
304 = "Encoding Error",
305 = "SSL Error",
# 500 - 900 client side
# 500 information
501 = "OK",
502 = "Version",
# 600 authentication and maintenance
601 = "Response",
602 = "Failure",
# 700 command
701 = "Reqiuest",
702 = "Cancel",
# 800 client
801 = "SSL Error"
)