def __init__(self, sink, port_in=9000, port_out=None): self.tcp_queue, self.sink_queue = Queue(), Queue() self.sink = sink self.tcp_in = TCPServer(port_in) if port_out: self.tcp_out = TCPServer(port_out) else: self.tcp_out = self.tcp_in self.workers = [] self.interrupt = threading.Event()
def dummy_tcp_server(): example_server = TCPServer() with example_server as tcp_server: thread = threading.Thread(target=example_server.listen_for_traffic) thread.daemon = True thread.start() yield example_server
def main() -> None: clern_server = TCPServer() if not (os.path.exists("./archives")): os.mkdir("./archives") # Looks for new archives collected t = ThreadPoolExecutor() t.submit(clern_server.listen_loop) # Performs all detection logic on received packets. detection_loop(clern_server) # Shut down listen loop t.shutdown()
class ShrVimServer(threading.Thread): """Main class. Attributes: _users_text_manager: Instance of UsersTextManager. _tcp_server: Instance of TCPServer. _cmd_ui: Instance of CmdUI. """ def __init__(self): """Constructor.""" super(ShrVimServer, self).__init__() try: self._args = _Args() except _ArgsError as e: raise _ShrVimServerError(str(e) + '\n' + _Args.DOCUMENT) self._users_text_manager = UsersTextManager(self._args.saved_filename) self._tcp_server = TCPServer(self._args.port, self._users_text_manager) self._cmd_ui = CmdUI(['load %s' % self._args.user_list_filename], self._users_text_manager, self._tcp_server, self) log.info.interface = self._cmd_ui log.error.interface = self._cmd_ui def run(self): """Starts the program.""" self._tcp_server.start() self._cmd_ui.start() self._cmd_ui.join() self._tcp_server.join() def stop(self): """Exits the program.""" self._cmd_ui.stop() self._tcp_server.stop()
def run(self): ldebug('starting MonAMI server thread', self) serversocket = TCPServer(self.address, self.port).listen() #serversocket.setblocking(0) if not serversocket: ldebug('server socket could not be bound', self) return 1 while self.runthread: try: client_socket, client_address = serversocket.accept() except socket.timeout as exception: # Socket timeout occured continue except socket.error as exception: lerror('socket error (%s): %s - ' % (exception, format_exc()), self) sleep(1) continue except: lerror('general error: %s - ' % format_exc(), self) sleep(1) continue ldebug('connected to %s:%d' % client_address, self) client_thread = MonAMIHandler(client_socket, client_address, self.event_socket) client_thread.deregister_at_server = self.deregister_handler_thread client_thread.user_password_authentication = self.user_password_authentication client_thread.start() if client_thread.isAlive(): self.register_handler_thread(client_thread) ldebug( 'registered handler threads: %d' % len(self.handler_threads), self) ldebug('exiting MonAMI server thread', self)
def __init__(self): """Constructor.""" super(ShrVimServer, self).__init__() try: self._args = _Args() except _ArgsError as e: raise _ShrVimServerError(str(e) + '\n' + _Args.DOCUMENT) self._users_text_manager = UsersTextManager(self._args.saved_filename) self._tcp_server = TCPServer(self._args.port, self._users_text_manager) self._cmd_ui = CmdUI(['load %s' % self._args.user_list_filename], self._users_text_manager, self._tcp_server, self) log.info.interface = self._cmd_ui log.error.interface = self._cmd_ui
def func1(port): data = [] s = TCPServer() clientAddr = s.start(port) print clientAddr len = s.read(data, None) print 'server read len =' + str(len) print ''.join(data) len = s.write("server write", None) print 'server write len =' + str(len) s.end()
def run(self): ldebug("starting MonAMI server thread", self) serversocket = TCPServer(self.address, self.port).listen() # serversocket.setblocking(0) if not serversocket: ldebug("server socket could not be bound", self) return 1 while self.runthread: try: client_socket, client_address = serversocket.accept() except socket.timeout as exception: # Socket timeout occured continue except socket.error as exception: lerror("socket error (%s): %s - " % (exception, format_exc()), self) sleep(1) continue except: lerror("general error: %s - " % format_exc(), self) sleep(1) continue ldebug("connected to %s:%d" % client_address, self) client_thread = MonAMIHandler(client_socket, client_address, self.event_socket) client_thread.deregister_at_server = self.deregister_handler_thread client_thread.user_password_authentication = self.user_password_authentication client_thread.start() if client_thread.isAlive(): self.register_handler_thread(client_thread) ldebug("registered handler threads: %d" % len(self.handler_threads), self) ldebug("exiting MonAMI server thread", self)
def main(): s = TCPServer() s.start(9989) path = "./1.txt" while True: dict = {} data = [] size = s.read(data, None) mydevice = IoTDevice.Sample.IoTDevice.IoTDevice.GetRootAsIoTDevice( data[0], 0) for i in xrange(mydevice.DataLength()): dict[mydevice.Data(i).Name()] = mydevice.Data(i).Value() json_val = json.dumps(dict) print "json_val = %s" % json_val f = open(path, 'w') f.write(json_val) f.write('\n') f.close() s.end()
def run(self): TCPServer()
#encoding: utf8 import sys sys.path.append("../common") from tcp_server import TCPServer import ptrace tcpServer=TCPServer() tcpServer.listen() tcpServer.run()
from tcp_server import TCPServer port = 3000 server = "192.168.111.119" server_address = (server, port) print("[Starting] Server is starting...") live = True tcp_server = TCPServer(server_address, live) tcp_server.start()
from PyQt5 import QtWidgets from UI.mainwindow import Ui_MainWindow from UI.plotwindow import Ui_PlotWindow import sys from PyQt5.QtCore import QThread, pyqtSignal, QTimer, Qt from tcp_server import TCPServer, Statistic # create instance of TCP Class server = TCPServer() # no send area x_range = list(range(1, 9)) y_range = list(range(7, 15)) stat = Statistic(x_range, y_range) class RunServerThread(QThread): # Signals for Gui client_changed = pyqtSignal(object) msg_received = pyqtSignal(object) finished = pyqtSignal() # run new Thread for server def __init__(self, parent=None): super().__init__(parent=None) self._actice = True def run(self): while self._actice:
class TCPBridge(object): SELECT_TIMEOUT = 0.1 def __init__(self, sink, port_in=9000, port_out=None): self.tcp_queue, self.sink_queue = Queue(), Queue() self.sink = sink self.tcp_in = TCPServer(port_in) if port_out: self.tcp_out = TCPServer(port_out) else: self.tcp_out = self.tcp_in self.workers = [] self.interrupt = threading.Event() def start(self, in_background=True): if not self.workers: try: self.interrupt.clear() self.tcp_in.initialize() self.workers.append( threading.Thread(target=self._run_tcp, args=(self.tcp_in, ))) if self.tcp_in != self.tcp_out: self.tcp_out.initialize() self.workers.append( threading.Thread(target=self._run_tcp, args=(self.tcp_out, ))) if in_background: self.workers.append( threading.Thread(target=self._run_sink)) for worker in self.workers: worker.start() if not in_background: self._run_sink() except TCPError as error: raise TCPBridgeError(str(error)) def stop(self): if self.workers: self.interrupt.set() for worker in self.workers: worker.join() self.workers = [] self.tcp_in.close() self.tcp_out.close() self.sink.close() def _run_tcp(self, endpoint): while not self.interrupt.is_set(): try: endpoint.accept_connection() while not self.interrupt.is_set(): self._process_endpoint_data(endpoint, self.sink_queue) if endpoint == self.tcp_out: self._process_queue(self.tcp_queue, endpoint) except TCPError: pass endpoint.close_client() def _run_sink(self): while not self.interrupt.is_set(): try: self._process_endpoint_data(self.sink, self.tcp_queue) self._process_queue(self.sink_queue, self.sink) except SinkError: self.interrupt.set() def _process_endpoint_data(self, endpoint, queue): try: r, w, e = select.select([endpoint.fileno], [], [], self.SELECT_TIMEOUT) if not r: return data = endpoint.read() if self.tcp_out.has_connection or endpoint == self.tcp_in: queue.put_nowait(data) except ValueError as error: raise SinkError(str(error)) @staticmethod def _process_queue(queue, endpoint): try: data = queue.get_nowait() endpoint.write(data) except Empty: pass