- 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:
parent
fbb4653740
commit
1207c6b225
6 changed files with 306 additions and 297 deletions
108
asrc-server.py
108
asrc-server.py
|
@ -25,7 +25,7 @@
|
||||||
# aSRC (Aliased Server Remote Control)
|
# aSRC (Aliased Server Remote Control)
|
||||||
# - SERVER -
|
# - SERVER -
|
||||||
#
|
#
|
||||||
# program version: 0.0.0.20130424
|
# program version: 0.0.0.20130426
|
||||||
# protocol version: 0.2.20130423
|
# protocol version: 0.2.20130423
|
||||||
#
|
#
|
||||||
#
|
#
|
||||||
|
@ -35,84 +35,12 @@
|
||||||
import sys, os, socket, socketserver, threading, time
|
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):
|
class ThreadedRequestHandler(socketserver.StreamRequestHandler):
|
||||||
|
|
||||||
def handle(self):
|
def handle(self):
|
||||||
|
"""
|
||||||
|
Handles incoming connections
|
||||||
|
"""
|
||||||
|
|
||||||
# Set time for timeout in seconds
|
# Set time for timeout in seconds
|
||||||
self.timeout = TIMEOUT
|
self.timeout = TIMEOUT
|
||||||
|
@ -121,20 +49,15 @@ class ThreadedRequestHandler(socketserver.StreamRequestHandler):
|
||||||
if VERBOSITY >=3:
|
if VERBOSITY >=3:
|
||||||
print("Client connected: " + str(self.client_address))
|
print("Client connected: " + str(self.client_address))
|
||||||
|
|
||||||
# send header line 1
|
|
||||||
self.request.sendall(bytes
|
|
||||||
("asrpc " + ProtocolVersion + "\n", ENCODING))
|
|
||||||
|
|
||||||
# send motd
|
# send motd
|
||||||
self.request.sendall(bytes(motd() + "\n", ENCODING))
|
self.request.sendall(comm.motd(MOTD) + "\n", ENCODING))
|
||||||
|
|
||||||
# Receive data
|
# Receive data
|
||||||
self.data = str(self.rfile.readline().strip(), ENCODING)
|
self.data = str(self.rfile.readline().strip(), ENCODING)
|
||||||
|
|
||||||
# content handler
|
# content handler
|
||||||
self.request.sendall(bytes(
|
self.request.sendall(bytes(
|
||||||
content.handler(str(self.client_address), self.data, aliases,
|
comm.handler(str(self.client_address), self.data), ENCODING))
|
||||||
ServerVersion, ProtocolVersion, VERBOSITY), ENCODING))
|
|
||||||
|
|
||||||
|
|
||||||
class ThreadedTCPServer(socketserver.ThreadingMixIn, socketserver.TCPServer):
|
class ThreadedTCPServer(socketserver.ThreadingMixIn, socketserver.TCPServer):
|
||||||
|
@ -142,26 +65,33 @@ class ThreadedTCPServer(socketserver.ThreadingMixIn, socketserver.TCPServer):
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
|
# import global settings
|
||||||
global VERBOSITY, HOST, PORT, TIMEOUT, ENCODING
|
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
|
parser = argparser
|
||||||
args = parser.parse(ServerVersion, ProtocolVersion)
|
args = parser.parse(ServerVersion, ProtocolVersion)
|
||||||
|
|
||||||
|
# look for pid file
|
||||||
if os.path.exists("pid"):
|
if os.path.exists("pid"):
|
||||||
|
# remove, ...
|
||||||
if args.delete_pid_file: os.remove("pid")
|
if args.delete_pid_file: os.remove("pid")
|
||||||
|
# ... ignore ...
|
||||||
elif args.allow_multiple_instances: pass
|
elif args.allow_multiple_instances: pass
|
||||||
|
# ... or exit
|
||||||
else:
|
else:
|
||||||
print(
|
print(
|
||||||
"\npid file already exists\n"\
|
"\npid file already exists\n"\
|
||||||
"If the server didn't shut down correctly, just delete this file "\
|
"If the server didn't shut down correctly, "\
|
||||||
"or pass -n\n"\
|
"just delete this file or pass -n\n"\
|
||||||
"If you want to start multiple instances, pass -m\n")
|
"If you want to start multiple instances, pass -m\n")
|
||||||
return 1
|
return 1
|
||||||
|
|
||||||
print(args)
|
# set settings if command line options are given
|
||||||
|
|
||||||
if args.host: HOST = args.host
|
if args.host: HOST = args.host
|
||||||
if args.port: PORT = args.port
|
if args.port: PORT = args.port
|
||||||
if args.timeout: TIMEOUT = args.timeout
|
if args.timeout: TIMEOUT = args.timeout
|
||||||
|
@ -193,9 +123,11 @@ def main():
|
||||||
ServerThread.daemon = True
|
ServerThread.daemon = True
|
||||||
ServerThread.start()
|
ServerThread.start()
|
||||||
|
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
time.sleep(10)
|
time.sleep(10)
|
||||||
|
|
||||||
|
# exit if Ctrl-C is pressed
|
||||||
except KeyboardInterrupt:
|
except KeyboardInterrupt:
|
||||||
print("\nGot Ctrl-C, shutting down server...")
|
print("\nGot Ctrl-C, shutting down server...")
|
||||||
|
|
||||||
|
|
Binary file not shown.
|
@ -1,2 +1,2 @@
|
||||||
from .argparser import argparser
|
from .argparser import argparser
|
||||||
from .content import content
|
from .comm import comm
|
||||||
|
|
|
@ -1,12 +1,16 @@
|
||||||
# includes/argparser.py
|
# includes/argparser.py
|
||||||
#
|
#
|
||||||
# module version: 1.0.20130424
|
# module version: 1.0.20130425
|
||||||
#
|
#
|
||||||
|
|
||||||
|
|
||||||
class argparser:
|
class argparser:
|
||||||
|
|
||||||
def parse(program_version, protocol_version):
|
def parse(program_version, protocol_version):
|
||||||
|
"""
|
||||||
|
Parses comand line arguments
|
||||||
|
"""
|
||||||
|
|
||||||
import argparse
|
import argparse
|
||||||
|
|
||||||
parser = argparse.ArgumentParser(
|
parser = argparse.ArgumentParser(
|
||||||
|
@ -45,34 +49,29 @@ class argparser:
|
||||||
"--verbosity",
|
"--verbosity",
|
||||||
type = int,
|
type = int,
|
||||||
choices = range(0, 4),
|
choices = range(0, 4),
|
||||||
# default = -1,
|
|
||||||
help = "increase output verbosity. Can be from 0 "\
|
help = "increase output verbosity. Can be from 0 "\
|
||||||
"(only default output) to 3 (debug messages).")
|
"(only default output) to 3 (debug messages).")
|
||||||
|
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
"-h",
|
"-h",
|
||||||
"--host",
|
"--host",
|
||||||
# default = -1,
|
|
||||||
help = "IP or hostname (use 0.0.0.0 for all interfaces) on which "\
|
help = "IP or hostname (use 0.0.0.0 for all interfaces) on which "\
|
||||||
"the server should listen")
|
"the server should listen")
|
||||||
|
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
"-p",
|
"-p",
|
||||||
"--port",
|
"--port",
|
||||||
# default = -1,
|
|
||||||
help = "the port on which the server should listen")
|
help = "the port on which the server should listen")
|
||||||
|
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
"-t",
|
"-t",
|
||||||
"--timeout",
|
"--timeout",
|
||||||
# default = -1,
|
|
||||||
help = "timeout of a connection in seconds - still "\
|
help = "timeout of a connection in seconds - still "\
|
||||||
"doesn't work obviously...")
|
"doesn't work obviously...")
|
||||||
|
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
"-e",
|
"-e",
|
||||||
"--encoding",
|
"--encoding",
|
||||||
# default = -1,
|
|
||||||
help = "encoding to be used when communicating with clients")
|
help = "encoding to be used when communicating with clients")
|
||||||
|
|
||||||
return parser.parse_args()
|
return parser.parse_args()
|
||||||
|
|
|
@ -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"\
|
"{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
|
# Look if the received message is an
|
||||||
# valid alias or a predefined command
|
# valid alias or a predefined command
|
37
include/statuscodes.py
Normal file
37
include/statuscodes.py
Normal 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"
|
||||||
|
)
|
Loading…
Reference in a new issue