Commit 97d3f719 authored by Yori 'AGy' Fournier's avatar Yori 'AGy' Fournier
Browse files

add the Server class now called by python -m myplotlib --server

parent 914c792f
......@@ -66,6 +66,8 @@ if D_HIERARCHY in ('CLIENT', 'client', 'LOCAL', 'local'):
elif D_HIERARCHY in ('SERVER', 'server'):
print(INFO+'Import myplotlib as a server.')
from .config import D_HOST, D_PORT
from socket import socket, AF_INET, SOCK_STREAM
else:
print(SEVR+'the value of D_HIERARCHY has to be SERVER or CLIENT')
......
......@@ -31,9 +31,15 @@
#
#
# IMPORT ---------------------------------------------------------------
from . import D_HIERARCHY
from . import INFO, SPCE, DBUG
from . import myTest, testList
from .test import __dict__ as availTests
if D_HIERARCHY in ('CLIENT', 'client', 'local', 'LOCAL'):
from . import myTest, testList
from .test import __dict__ as availTests
else:
from socket import socket, AF_INET, SOCK_STREAM
from . import D_HOST, D_PORT
import sys
import getopt
......@@ -56,7 +62,7 @@ usage = '''
# GET OPT
try:
opts, args = getopt.getopt(
sys.argv[1:], "htp:", ["test=", "plot=", "args=", "debug"])
sys.argv[1:], "htp:", ["test=", "plot=", "args=", "debug", "server"])
except getopt.GetoptError:
print(usage)
......@@ -90,6 +96,9 @@ for opt, arg in opts:
elif opt in ("--debug"):
debug = True
elif opt in ("--server"):
run4server = True
# SECTION: TESTS ------------------------------------------------------
if run4Test:
......@@ -137,6 +146,14 @@ elif run4Plot:
# print(INFO+"To start interactively:")
# print(SPCE+">> from mfdPlot import * \n")
elif run4server:
from .server import Server
server = Server()
server.init()
server.run()
else:
print(usage)
......
from . import socket, AF_INET, SOCK_STREAM
from . import D_HOST
from . import D_PORT
class Server():
def __init__(self):
self.soc = socket(AF_INET, SOCK_STREAM)
self.host = D_HOST
self.port = D_PORT
self.answer = None
def init(self):
self.soc.bind((self.host, self.port))
self.soc.listen(1)
def run(self):
while True:
if not self.answer:
conn, addr = self.soc.accept()
print(INFO + 'Connected by ' + str(addr))
self.answer = conn.recv(1024)
if self.answer in ('KILL', 'kill', 'Kill'):
print(INFO+"Recieved 'Kill' signal.")
conn.close()
break
else:
self.treatAnswer()
def treatAnswer(self):
try:
exec(str(self.answer))
except:
print(INFO+"I could not execute the command")
print(INFO+'Received: '+str(self.answer))
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment