def runserver(self): """ Загрузка параметров командной строки, если нет параметров, то задаём значения по умоланию. Сначала обрабатываем порт: server.py -p 8079 -a 192.168.1.2 """ try: if "-p" in sys.argv: listen_port = int(sys.argv[sys.argv.index("-p") + 1]) else: listen_port = s.DEFAULT_PORT if listen_port < 1024 or listen_port > 65535: raise ValueError except IndexError: print("После параметра -'p' необходимо указать номер порта.") sys.exit(1) except ValueError: print( "В качастве порта может быть указано только число в диапазоне от 1024 до 65535." ) sys.exit(1) # Затем загружаем какой адрес слушать try: if "-a" in sys.argv: listen_address = sys.argv[sys.argv.index("-a") + 1] else: listen_address = "" except IndexError: print( "После параметра 'a'- необходимо указать адрес, который будет слушать сервер." ) sys.exit(1) server.create_socket(listen_address, listen_port)
def server_options(): global option, ip, port while True: try: with open("options.json") as f: ff = json.load(f) if ff[0]["ip"] != "" and ff[0]["port"] != 0: ip, port = ff[0]["ip"], ff[0]["port"] print( f"USED IP, PORT: {ip, port}\n1 - Join Server\n2 - Create Server\n3 - Change ip/port\n4 - Back\n" ) option = int(input(">")) # Join server if option == 1: _ip, _port = add_ip_port() if connect_to_server(_ip, _port): return True # Create server elif option == 2: if server.create_socket(ip, port): if connect_to_server(ip, port): threading.Thread(target=server.start).start() return True # Change ip and port elif option == 3: _ip, _port = add_ip_port() with open("options.json", "w") as f: json.dump([{"ip": _ip, "port": _port}], f) elif option == 4: return False else: print("Please type one of the options") except ValueError: print("Please type a number\n")
serverPort = sys.argv[2] # If server was informed in the commandline argument if len(serverPort.split(':')) == 2: serverPort = int(serverPort.split(':')[1]) server_ip = serverPort.split(':')[0] else: serverPort = int(serverPort) server_ip = '0.0.0.0' except ValueError: print 'Server port must be an integer.' sys.exit() if serverPort > 65535: print 'Server port must be lower than 65535.' else: client_socket = create_socket(serverPort, server_ip) print 'Connected to chat server' while True: try: SOCKET_LIST = [sys.stdin, client_socket] # Get the list sockets which are readable read_sockets, write_sockets, error_sockets = select.select( SOCKET_LIST, [], []) except KeyboardInterrupt: print "\nQuitting..." h_msg = DISCONNECT msg_enc = EncodeAES(CIPHER, h_msg) client_socket.send(msg_enc) sys.exit() for sock in read_sockets:
print("Matching Files") if Hash1 != Hash2: print("Changed") print('updated') Hash1 = Hash2 updated = open('signatures.txt', 'r') Update = updated.readlines() i = 0 j = 0 updated.close() # print (listOfPeers) while (not (compare(listOfPeers, server.updatedPeers))): server.create_socket() server.socket_bind() print("Accept socket now") server.socket_accept() print("Socket Accepted") print("Going to send file") server.main_menu() print(server.updatedPeers) # global server.updatedPeers server.updatedPeers = [] # server.refresh_connections() else: for x in listOfPeers: cl.strHost = str(x)
def p_open_server(p): """open_server : OPEN_SERVER""" server.create_socket() server.bind(server_socket) server.listen(server_socket) client.connect(client_socket)