def checkSmtp(): """ Make a connection socket test into STMP server. Return status code of SocketType.connect_ex(). """ try : S = SocketType() S.settimeout(2) smtpStatus = S.connect_ex( ( settings.SNAFU['smtp-server'],settings.SNAFU['smtp-port']) ) S.close() except (error,gaierror), e : smtpStatus = e return smtpStatus
def is_on(self): """ Make a connection socket test. """ from urlparse import urlsplit S = SocketType() S.settimeout(2) errno = S.connect_ex((urlsplit(settings.SNAFU['glpi-url']).netloc, 80)) S.close() if errno < 0 : return False return True
def run(self, sock: SocketType) -> typing.Any: "reference implementation of how to deal with socket" self.send(b"") while True: for to_send, close, exc, result in self: if to_send: sock.sendall(to_send) if close: sock.close() if exc: raise exc if result is not _no_result: return result data = sock.recv(1024) if not data: raise ParseError("need data") self.send(data)
class Admin: def __init__( self, server="localhost", port=config.port, password=None ): self.server = server self.port = port if password: self.password = md5(password).hexdigest() else: self.password = None self.socket = SocketType() def connect( self ): self.socket.connect( ( self.server, self.port ) ) def sendMessage( self, message ): if self.password: self.socket.send( message+" "+self.password+"\n" ) else: self.socket.send( message+"\n" ) def quit( self ): self.socket.close()
class Admin: def __init__(self, server="localhost", port=config.port, password=None): self.server = server self.port = port if password: self.password = md5(password).hexdigest() else: self.password = None self.socket = SocketType() def connect(self): self.socket.connect((self.server, self.port)) def sendMessage(self, message): if self.password: self.socket.send(message + " " + self.password + "\n") else: self.socket.send(message + "\n") def quit(self): self.socket.close()
def _handle_client(self, client_socket : socket.SocketType): print("Connection started: %s" % client_socket) try: while True: request = client_socket.recv(BUFFERSIZE).decode('utf-8') print("Got a request: %s (length: %s)" % (request, len(request))) if request.startswith("ECHO "): print("SEND ECHO") client_socket.send(request[len("ECHO "):].encode()) elif request.startswith("EXEC "): cmd = request[(len("EXEC ")):] result = 'EXEC RESULT: %s' % '\n'.join(os.popen(cmd).readlines()) print("THE LINES: %s" % result) client_socket.send(result.encode()) else: print('Close connection: %s' % request) client_socket.close() return except socket.error: pass print('Closing socket.') client_socket.close()
def __wait_for_client_connections(server_socket: socket.SocketType, server_addr, no_clients, timeout, max_games): '''Sets the timeout and waits for client connections. If the socket timesout the sever closes sockets and exits''' server_socket.settimeout(float(timeout)) clients = [] start = time.perf_counter() while len(clients) < no_clients * max_games: start = time.perf_counter() try: conn, addr = server_socket.accept() __send_server_welcome(conn, server_addr[0]) client_info = __request_client_name(conn, clients) clients.append(client_info) except socket.timeout: end = time.perf_counter() dur = int(end - start) for client in clients: client[CONN].close() print( 'Timeout: {} seconds have passed since last player connected'. format(dur)) server_socket.close() exit() return clients