def StartServer(self): ## after all requirements are met # we create and activate our TCPServer Class # input("\n ENTER ANY KEY TO START SERVER :") s.ntf("\n---------------starting server---------------") self.server = TCPServer(self.host, self.port) self.server.start_server()
class WServerForm(QMainWindow): def __init__(self, parent=None): super(WServerForm, self).__init__(parent) self.ui = Ui_MainWindow() self.ui.setupUi(self) # un clic sur le bouton appellera la méthode 'action_bouton' self.ui.pushButton_StartStop.clicked.connect(self.action_start_stop) # on affiche un texte en bas de la fenêtre (status bar) self.ui.statusbar.showMessage("Hello") self.__port_number = 8080 self.__started = False self.__server = None def action_start_stop(self): if (self.__started): self.__started = False self.__server.stop() self.ui.textEdit_Resume.insertPlainText("Server stop on port : " + str(self.__port_number) + "\n") self.ui.pushButton_StartStop.setText("Start") self.ui.lineEdit_portNumber.setEnabled(True) self.__server = None self.ui.lcdNumber_NbClients.display(0) else: self.__started = True self.__port_number = int(self.ui.lineEdit_portNumber.text()) self.ui.pushButton_StartStop.setText("Stop") self.ui.lineEdit_portNumber.setEnabled(False) self.__server = TCPServer(('127.0.0.1', self.__port_number)) self.__server.evt_server_clientConnected += self.onClientConnected self.__server.evt_server_clientDisconnected += self.onClientDiconnected self.__server.evt_server_clientReceive += self.onClientReceive self.ui.textEdit_Resume.insertPlainText("Server start on port : " + str(self.__port_number) + "\n") self.__server.start() def onClientConnected(self, sender, message): if (not (self.__server is None)): self.ui.textEdit_Resume.insertPlainText(message + "\n") self.ui.lcdNumber_NbClients.display( self.__server.numberOfConnexion) def onClientDiconnected(self, sender, message): if (not (self.__server is None)): self.ui.textEdit_Resume.insertPlainText(message + "\n") self.ui.lcdNumber_NbClients.display( self.__server.numberOfConnexion) def onClientReceive(self, sender, message): if (not (self.__server is None)): self.ui.textEdit_Resume.insertPlainText(message + "\n")
def main(argv=None): if argv is None: argv = sys.argv params = parse_args() config_logging() print params handler = ServerHandler() init_raspberry_pins() server = TCPServer(params.host, params.port, handler, activate=True,) # max_connections=params.max_clients) server.start()
def action_start_stop(self): if (self.__started): self.__started = False self.__server.stop() self.ui.textEdit_Resume.insertPlainText("Server stop on port : " + str(self.__port_number) + "\n") self.ui.pushButton_StartStop.setText("Start") self.ui.lineEdit_portNumber.setEnabled(True) self.__server = None self.ui.lcdNumber_NbClients.display(0) else: self.__started = True self.__port_number = int(self.ui.lineEdit_portNumber.text()) self.ui.pushButton_StartStop.setText("Stop") self.ui.lineEdit_portNumber.setEnabled(False) self.__server = TCPServer(('127.0.0.1', self.__port_number)) self.__server.evt_server_clientConnected += self.onClientConnected self.__server.evt_server_clientDisconnected += self.onClientDiconnected self.__server.evt_server_clientReceive += self.onClientReceive self.ui.textEdit_Resume.insertPlainText("Server start on port : " + str(self.__port_number) + "\n") self.__server.start()
def run_server_process(): print("start run_server_process pid:" + str(os.getpid())) # get config object config = ConfigReader.get_config() # init tcp server tcp_server = TCPServer.TCPServer() # init udp server udp_server = UDPServer.UDPServer() # start event loop event_loop = asyncio.get_event_loop() udp_server.start_server(event_loop) tasks = [tcp_server.start_server()] event_loop.run_until_complete(asyncio.wait(tasks)) try: event_loop.run_forever() except KeyboardInterrupt: pass finally: event_loop.close()
def main(): if len(sys.argv) < 4: print("Usage: python3 myscript.py <server|client> <IP> <PORT>") else: if sys.argv[1] == "server": server = TCPServer((sys.argv[2], int(sys.argv[3])), echo_request) server.start_server() print('Server Started') print('Press CTRL+D to stop') while True: try: input() except EOFError: server.stop_server() print('Server stopped') break elif sys.argv[1] == "client": client = Client() client.connect((sys.argv[2], int(sys.argv[3]))) client.talk_to_sever()
from server import TCPServer, NEW_MESSAGE, NEW_CONNECTION import sys if not (len(sys.argv) == 3 and sys.argv[2].isdigit()): sys.exit(0) server = TCPServer(ip=sys.argv[1], port=int(sys.argv[2])) @server.on(NEW_CONNECTION) def on_incoming_connection(*args, **kwargs): print(f"New connection established {args}") @server.on(NEW_MESSAGE) def on_new_message(addr, message, **kwargs): print(f"Message from addr {addr}: {message}") server.broadcast(message) def main(): print("Starting server...") server.start() print("Server started! Waiting for incoming connections!") if __name__ == "__main__": main()
import socket import argparse from server import TCPServer parser = argparse.ArgumentParser(description='OldTrusty Server') parser.add_argument('-ip', '--ip', nargs=1, help='Optional, IP') args = parser.parse_args() if(args.ip is not None): host=''.join(args.ip) else: host='127.0.0.1' server = TCPServer(host, 3002) server.serve_forever()
from server import TCPServer ts = TCPServer(host="127.0.0.1", port=51234) ts.run()
__author__ = 'tung' from server import TCPServer from server import UDPServer server=TCPServer() hashkey=server.startauth_handshake() if hashkey is not None: print ('Hand shake completed. Data transmission initiated via UDP Protocol...') #Start UDP transmission if user is verified udpserver =UDPServer() udpserver.startTransmission(hashkey)
import signal from requests_handlers import make_initial_request, make_request from primitives.thread_pool import ThreadPool from data_handlers import get_aggregate_data from primitives.queue import Queue from server import TCPServer if __name__ == "__main__": access_token, url = make_initial_request() q_out = Queue() q_in = Queue() q_in.put(url) with ThreadPool(max_workers=6) as executor: for _ in range(6): executor.submit(make_request, access_token, q_in, q_out) data = get_aggregate_data(q_out) server = TCPServer() server.start(data)
class ServerInterface: ## chat room class ## this initiates the server and someone else can ## also connect through running client .py def __init__(self): self.port = configs.port self.host = host self.server = None self.running = True def validate_host(self, host): # validate if a host is real by creating and closing # a test socket try: sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.bind((host, self.port)) sock.shutdown() sock.close() s.ntf("valid host") return True except: s.ntf("invalid host ..please check again ") return False def StartServer(self): ## after all requirements are met # we create and activate our TCPServer Class # input("\n ENTER ANY KEY TO START SERVER :") s.ntf("\n---------------starting server---------------") self.server = TCPServer(self.host, self.port) self.server.start_server() def ChatRoomInterface(self): ## interface for server setup # print(intro) ch = input("\ndo you wish to change the host ?? Y/N :") ## if you dont want to change anything # just enter 'M' while self.running == True: if ch == 'Y': host = input("\nNEW HOST (name or IP address):") if host == 'X': ch = 'N' elif self.validate_host(host) == True: self.host = host self.StartServer() break else: print( f"\npress X to go back\n\nINVALID HOSTNAME [{host}] is it connected ?" ) if ch == 'N': self.StartServer() break else: self.ChatRoomInterface() break