1
0
Fork 0
mirror of https://github.com/de2tla2f/pymaster.git synced 2024-11-25 12:21:28 +00:00

pymaster: version 2, with help, possibility to change IP:port from cmdline and daemon mode

This commit is contained in:
Alibek Omarov 2019-10-17 04:04:23 +03:00
parent 5aa6d9a644
commit 70c1b50c3f

View file

@ -1,46 +1,31 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
# Basic networking
import socket import socket
# Challenge generator
import random import random
# System important... things
import sys import sys
import traceback import traceback
import logging import logging
import os
# Network packet creating from optparse import OptionParser
from struct import pack from struct import pack
# Server time control
from time import time from time import time
# ServerEntry class module
from server_entry import ServerEntry from server_entry import ServerEntry
# Protocol class
from protocol import MasterProtocol from protocol import MasterProtocol
UDP_IP = "0.0.0.0"
UDP_PORT = 27010
LOG_FILENAME = 'pymaster.log' LOG_FILENAME = 'pymaster.log'
logging.getLogger().addHandler(logging.StreamHandler())
logging.getLogger().addHandler(logging.FileHandler(LOG_FILENAME))
logging.getLogger().setLevel(logging.DEBUG)
def logPrint( msg ): def logPrint( msg ):
logging.debug( msg ) logging.debug( msg )
class PyMaster: class PyMaster:
serverList = [] def __init__(self, ip, port):
sock = socket.socket( socket.AF_INET, socket.SOCK_DGRAM ) self.serverList = []
self.sock = socket.socket( socket.AF_INET, socket.SOCK_DGRAM )
def __init__(self): self.sock.bind( (ip, port) )
self.sock.bind( (UDP_IP, UDP_PORT) )
logPrint("Welcome to PyMaster!") logPrint("Welcome to PyMaster!")
logPrint("I ask you again, are you my master?") logPrint("I ask you again, are you my master?")
logPrint("Running on {0}:{1}".format( UDP_IP, UDP_PORT)) logPrint("Running on %s:%d" % (ip, port))
def serverLoop(self): def serverLoop(self):
data, addr = self.sock.recvfrom(1024) data, addr = self.sock.recvfrom(1024)
@ -181,11 +166,13 @@ class PyMaster:
serverEntry.setInfoString( serverInfo ) serverEntry.setInfoString( serverInfo )
def main( argv = None ): def spawn_pymaster(verbose, ip, port):
if argv is None: if verbose:
argv = sys.argv logging.getLogger().addHandler(logging.StreamHandler())
logging.getLogger().addHandler(logging.FileHandler(LOG_FILENAME))
logging.getLogger().setLevel(logging.DEBUG)
masterMain = PyMaster() masterMain = PyMaster(ip, port)
while True: while True:
try: try:
masterMain.serverLoop() masterMain.serverLoop()
@ -194,4 +181,22 @@ def main( argv = None ):
pass pass
if __name__ == "__main__": if __name__ == "__main__":
sys.exit( main( ) ) parser = OptionParser()
parser.add_option('-i', '--ip', action='store', dest='ip', default='0.0.0.0',
help='ip to listen [default: %default]')
parser.add_option('-p', '--port', action='store', dest='port', type='int', default=27010,
help='port to listen [default: %default]')
parser.add_option('-d', '--daemonize', action='store_true', dest='daemonize', default=False,
help='run in background, argument is uid [default: %default]')
parser.add_option('-q', '--quiet', action='store_false', dest='verbose', default=True,
help='don\'t print to stdout [default: %default]')
(options, args) = parser.parse_args()
if options.daemonize != 0:
from daemon import pidfile, DaemonContext
with DaemonContext(stdout=sys.stdout, stderr=sys.stderr, working_directory=os.getcwd()) as context:
spawn_pymaster(options.verbose, options.ip, options.port)
else:
sys.exit(spawn_pymaster(options.verbose, options.ip, options.port))