def cmd(self, command, quiet=False): reply = None try: if (not quiet): print("Sent:{}".format(command)) send_command = (self._HEAD + "{}".format(command)) serverAddressPort = ("127.0.0.1", self._SERVER_PORT) bufferSize = 1024 sock = socket(family=AF_INET, type=SOCK_DGRAM) socket.settimeout(sock, 4) sock.sendto(six.b(send_command), serverAddressPort) reply = sock.recvfrom(bufferSize) reply = reply[0][4:].decode() except: print("Unable to connect to server") finally: sock.close() if (reply == None): return "No Response" elif (reply.startswith("disconnect")): print("got a disconnect response") if (not quiet): print("Reply:{}".format(reply)) return reply
def start(keystore, keystorealias, keypass, keystorepassphrase, hostandport, timeout): verify(keystore, keystorealias, keypass, keystorepassphrase, hostandport) sslContext = getOpensslContext(keystore, keystorealias, keypass, keystorepassphrase) socket = socket_any_family() socket.settimeout(timeout) hostandport = hostandport.split(":", 2) socket.connect((hostandport[0], int(hostandport[1]))) sslConnection = Connection(sslContext, socket) sslConnection.set_connect_state() sslConnection.set_tlsext_host_name(hostandport[0].encode('utf-8')) while True: try: sslConnection.do_handshake() except OpenSSL.SSL.WantReadError: rd, _, _ = select.select([socket], [], [], socket.gettimeout()) if not rd: raise timeout('select timed out') continue except OpenSSL.SSL.Error as e: raise ssl.SSLError('bad handshake: %r' % e) break sslContext.set_timeout(timeout) sslConnection.send(b"GET / HTTP/1.0\r\n\r\n") socket.recv(1024)
def run(self, socket): socket.settimeout(3) # 设置超时时间 while self.__running: try: print("正在忙.....") socket.recv(8192) sleep(1) break except Exception: print("超时处理") break
def await_reponse(socket): running = True socket.settimeout(1) while running: try: data, client_conn = socket.recvfrom(2048) print("\n" * 50) print(data) print("") except timeout: continue except (KeyboardInterrupt, SystemExit): self.__running__ = False break
def rcon(self, command, quiet=False): reply = None try: if (not quiet): print("Sent:{}".format(command)) send_command = (self._HEAD + "rcon {} {}".format(self._RCON_PASSWORD, command)) serverAddressPort = ("127.0.0.1", self._SERVER_PORT) bufferSize = 1024 sock = socket(family=AF_INET, type=SOCK_DGRAM) socket.settimeout(sock, 4) sock.sendto(six.b(send_command), serverAddressPort) reply = sock.recvfrom(bufferSize) reply = reply[0][4:].decode() except: print("Unable to connect to server RCON") finally: sock.close() if reply.startswith("print\nbad rconpassword"): print("Incorrect rcon password.") elif (reply.startswith("disconnect")): print("got a disconnect response") elif not reply.startswith("print"): print( "Unexpected error while contacting server for the first time.") if (not quiet): print("Reply:{}".format(reply)) return reply
from socket import AF_INET, SOCK_STREAM, socket from zadania10.functions import get_command socket = socket(AF_INET, SOCK_STREAM) socket.connect(("localhost", 125)) socket.settimeout(10) print(socket.recv(1024)) socket.sendall(b"list_images \r\n") list_files = socket.recv(1024).replace(b"List: ", b"") splt_list_files = list_files.split(b"$") for i, j in zip(splt_list_files, range(len(splt_list_files))): print(str(j + 1) + ":", i.decode("utf-8")) file_name = b"" while True: try: numFile = input("Choose file: ") if int(numFile) < 1: continue file_name = splt_list_files[int(numFile) - 1] except (IndexError, ValueError): continue break socket.sendall(b"get_image: " + file_name + b" \r\n") f_command = get_command(socket) if f_command == b"Not properly file_path!!!\r\n": print(f_command)
def await_reponse(socket, textbox, buttons, main): global running socket.settimeout(1) while running: try: data, client_conn = socket.recvfrom(2048) result = json.loads(data.decode()) print(data) if result.get("code"): if result.get("code") == codes.FULL: textbox.setText("\nThe run is full!\nWait and try again") main.exit.setEnabled(True) main.replay.setEnabled(True) main.replay.setText("Try again") if result.get("code") == codes.IS_RUNNING: textbox.setText( "\nThe gamming is running!\nWait some minutes and try again!" ) main.exit.setEnabled(True) main.replay.setEnabled(True) main.replay.setText("Try again") elif result.get("code") == codes.REGISTRED: textbox.setText("Resgistred!\n\n Wait for more players...") elif result.get("code") == codes.QUESTION: msg = result["body"]["question"] + "\r\n" textbox.setText(msg) used_alterantives = [] for alternative in result["body"]["alternatives"]: btn = buttons.get(alternative["code"]) if btn: used_alterantives.append(alternative["code"]) btn.setText("\n" + alternative["text"]) btn.setEnabled(True) for k, v in buttons.items(): if k not in used_alterantives: v.setText("") v.setEnabled(False) elif result.get("code") == codes.FINISH_ROUND: textbox.setText(result["body"]) for k, v in buttons.items(): v.setText("") v.setEnabled(False) elif result.get("code") == codes.RESULT_RANK: players = list(result["body"].items()) players.sort(key=lambda x: x[1], reverse=True) for k, v in buttons.items(): v.setText("") v.setEnabled(False) msg = "Scores\n\n" for pos, (name, points) in enumerate(players): msg += "{0}º) \t {1} \t {2} points\n".format( pos + 1, name, points) textbox.setText(msg) main.exit.setEnabled(True) main.replay.setEnabled(True) main.replay.setText("Replay") running = False break except timeout: continue except (KeyboardInterrupt, SystemExit): break
from socket import socket, AF_INET, SOCK_DGRAM, timeout socket = socket(AF_INET, SOCK_DGRAM) socket.settimeout(1) socket.sendto(b"zad14odp;src;60788;dst;2901;data;programming in python is fun", ("localhost", 2019)) print(socket.recv(1024)) socket.sendto(b"zad14odp;src;60788;dst;2901;data;programmig in python is fun", ("localhost", 2019)) print(socket.recv(1024)) socket.sendto(b"zad14odp;src;60788;dst;2901;dta;programming in python is fun", ("localhost", 2019)) print(socket.recv(1024))