def run_client(server_address, server_port):
	# Ping a UDP pinger server running at the given address
	
	try:
		client_socket = Socket(socket.AF_INET, socket.SOCK_DGRAM)

		# Time out after 1 second
		client_socket.settimeout(1.0)

		print("Pinging", str(server_address), "on port", server_port)

		for i in range(10):
			client_socket.sendto("".encode(), (server_address, server_port))

			try:
				time_start = time.time()
				_, _ = client_socket.recvfrom(1024)
				time_stop = time.time()
			except socket.timeout:
				print("Packet lost")
			else:
				print("RTT(Round trip time): {:.3f}ms".format((time_stop - time_start) * 1000))
	finally:
		client_socket.close()

	return 0
def send_mail(sender_address, mail_server, receiver_address, message):
	try:
		client_socket = Socket(socket.AF_INET, socket.SOCK_STREAM)

		# set a 1 second timeout
		client_socket.settimeout(1.0)

		# connect to mail server
		client_socket.connect((mail_server, 25))

		def send(string):
			"""Helper function: fix newlines, encode and send a string"""
			final = string.replace("\n", "\r\n").encode("ascii")
			print "Sending " + final + "..."
			client_socket.send(final)

			return 0

		def recv_and_check(expected=250):
			"""Helper function: recive reply and check it's ok"""
			reply = client_socket.recv(2048)
			print "Got: ", reply

			code = int(reply.rstrip().split()[0])
			if code != expected:
				raise Exception(reply)

			return 0

		# get initial message from server
		recv_and_check(220)

		# send greeting
		send("HELO {}\n".format(sender_address.split("@")[1]))
		recv_and_check()

		# set sender address
		send("MAIL FROM: {}\n".format(sender_address))
		recv_and_check()

		# set receiver address
		send("RCPT TO: {}\n".format(receiver_address))
		recv_and_check()

		# prepare to send message
		send("DATA\n")
		recv_and_check(354)

		# send the message itself followed by terminator
		send("{}\n.\n".format(message))
		recv_and_check()

		send("QUIT\n")
		recv_and_check(221)

	finally:
		client_socket.close()


	return 0
Exemplo n.º 3
1
def create_broadcast_sockets():
    sender = Socket(AF_INET, SOCK_DGRAM)
    sender.setsockopt(SOL_SOCKET, SO_REUSEADDR, True)
    sender.setsockopt(SOL_SOCKET, SO_BROADCAST, True)
    sender.bind(("", 0))
    receiver = Socket(AF_INET, SOCK_DGRAM)
    receiver.settimeout(constants.broadcast_receiver_timeout)
    receiver.setsockopt(SOL_SOCKET, SO_REUSEADDR, True)
    receiver.setsockopt(SOL_SOCKET, SO_BROADCAST, True)
    receiver.bind(("", constants.broadcast_port))
    return sender, receiver
Exemplo n.º 4
1
    def _is_client_request_ssl(cls, socket_: socket.socket) -> bool:
        while True:
            original_timeout = socket_.gettimeout()
            socket_.setblocking(False)

            try:
                data = socket_.recv(3, socket.MSG_PEEK)
            except OSError as error:
                if error.errno in (errno.EWOULDBLOCK, errno.EAGAIN):
                    yield from asyncio.sleep(0.01)
                else:
                    raise
            else:
                break
            finally:
                socket_.settimeout(original_timeout)

        _logger.debug('peeked data %s', data)
        if all(ord('A') <= char_code <= ord('Z') for char_code in data):
            return False
        else:
            return True
Exemplo n.º 5
1
def run_client(server_address, server_port):
    """Ping a UDP pinger server running at the given address
    """

    client_socket = Socket(socket.AF_INET, socket.SOCK_DGRAM)

    message = "Ping"

    client_socket.settimeout(1)

    for i in range(0, 10):
        start_time = time.time()
        client_socket.sendto(message, (server_address, server_port))
        try:
            data, server = client_socket.recvfrom(1024)
            end_time = time.time()
            elapsed = end_time - start_time
            print "%s, time: %f." % (data, elapsed)
        except:
            print "TIMEOUT!"
        time.sleep(1)
    return 0
Exemplo n.º 6
1
def get_rtt(server_address, server_port):
    """Ping a UDP ping server running at the given address
    and show the ping status
    """
    server = (server_address, server_port)
    message = bytes("", 'UTF-8')

    client_socket = Socket(socket.AF_INET, socket.SOCK_DGRAM)
    client_socket.settimeout(1.0)

    try:
        initial_time = current_milli_time()
        client_socket.sendto(message, server)
        client_socket.recvfrom(1024)
        final_time = current_milli_time()

        rtt = final_time - initial_time
    except:
        rtt = -1

    client_socket.close()

    return rtt
Exemplo n.º 7
1
    def __demodulate(self, connection: socket.socket):
        connection.settimeout(0.1)
        time.sleep(self.TIMEOUT)

        total_data = []
        while True:
            try:
                data = connection.recv(65536)
                if data:
                    total_data.append(data)
                else:
                    break
            except socket.timeout:
                break

        if len(total_data) == 0:
            logger.error("Did not receive any data from socket.")

        arr = np.array(np.frombuffer(b"".join(total_data), dtype=np.complex64))
        signal = Signal("", "")
        signal._fulldata = arr
        pa = ProtocolAnalyzer(signal)
        pa.get_protocol_from_signal()
        return pa.plain_bits_str
Exemplo n.º 8
1
class Bus(common.AutoClose):
    """
    An Autobus bus. Busses manage a set of published services, and allow
    connecting to other services. A single bus listens on a single TCP
    port and multiplexes all published services over it.
    
    Bus is a subclass of ServiceProvider; the service it provides is a service
    exposing information about what other services, events, functions, and
    objects are present. (This service is more commonly known as the
    introspection service.) You normally won't have to know this; instances of
    Bus register themselves as services with themselves, so you don't need to
    do anything to make the introspection service work.
    """
    def __init__(self, default_discoverers=True, default_publishers=True,
                 port=None):
        """
        Creates a new bus. The bus will listen on the specified port; if none
        is specified (which is the usual case), a port will be chosen from the
        ports not currently in use on this computer.
        
        If default_discoverers is True (the default), a default set of
        discoverers will be installed, and likewise for default_publishers.
        Right now, this simply installs a autobus2.discovery.BroadcastPublisher
        and autobus2.discovery.BroadcastDiscoverer. Others might be added in
        the future.
        """
        # Number of times this bus has been __enter__'d. Allows it to be used
        # as a re-entrant context manager.
        self.context_enters = 0
        if port is None:
            port = 0
        # True once close() has been called
        self.closed = False
        # The TCP server that will listen for connections
        self.server = Socket()
        self.server.bind(("", port))
        # TODO: make the backlog configurable
        self.server.listen(100)
        self.port = self.server.getsockname()[1]
        # Lock that nearly everything bus-related locks on
        self.lock = RLock()
        # PropertyTable whose keys are service ids and whose values are
        # instances of autobus2.local.LocalService
        self.local_services = PropertyTable()
        self.local_services.global_watch(self.local_service_changed)
        # Map of ids of discovered services to DiscoveredService instances
        self.discovered_services = {}
        self.discovery_listeners = []
        # List of (filter, function) tuples, where filter is an info object
        # filter and function is a function to be notified when a matching
        # service is created or deleted
        self.service_listeners = []
        # Set of RemoteConnection instances that have been bound to a service
        self.bound_connections = set()
        # Set of discoverers registered on this bus
        self.discoverers = set()
        # Set of publishers registered on this bus
        self.publishers = set()
        if default_discoverers:
            self.install_discoverer(discovery.BroadcastDiscoverer())
        if default_publishers:
            self.install_publisher(discovery.BroadcastPublisher())
        Thread(name="autobus2.Bus.accept_loop", target=self.accept_loop).start()
        # Disable the introspection service for now. I'm seeing what would
        # happen if I have per-service introspection functions and objects, so
        # I'm disabling the bus-wide introspection service.
        # self._create_introspection_service()
        #
        # Register the bus as a service on itself.
        self.create_service({"type": "autobus.details", "pid": os.getpid()}, _IntrospectionService(self))
    
    def accept_loop(self):
        """
        Called on a new thread to accept socket connections to this bus.
        """
        self.server.settimeout(1)
        while not self.closed:
            try:
                socket = None
                socket = self.server.accept()[0]
                self.setup_inbound_socket(socket)
            except SocketTimeout: # This happens when we time out, which is
                # normal. The 1-second timeout is to fix what appears to be a
                # bug with Windows not properly throwing an exception from
                # accept when another thread closes the socket.
                pass
            except: # This happens when the server socket is closed
                if socket:
                    socket.close() # Make sure it's /really/ closed on the
                    # off chance that something else caused the exception
                if not issubclass(sys.exc_type, SocketError): # Something else
                    # happened
                    print_exc()
                # print "Bus server died"
                return
    
    @synchronized_on("lock")
    def create_service(self, info, provider):
        """
        Creates a new service on this bus. info is the info object to use for
        this service. provider is the instance of
        autobus2.service.ServiceProvider to publish; an instance of
        autobus2.providers.PyServiceProvider can be used to publish a simple
        Python object as a service. (This is how I expect most services to be
        published; writing a custom ServiceProvider subclass should rarely be
        needed.)
        
        The return value is an instance of local.LocalService. You can safely
        ignore it if you don't need it and don't plan on deleting the service
        before you close the bus itself.
        """
        # Create a new id for the service
        service_id = messaging.create_service_id()
        self.set_remote_info_builtins(service_id, info)
        # Create the actual service object
        service = local.LocalService(self, service_id, info, provider)
        # Then store the service in our services map, which will cause the
        # service to be published through the introspection service and through
        # the bus's publishers (see self.local_service_changed).
        self.local_services[service_id] = service
        return service
    
    def _close_service(self, service):
        # This is called from LocalService.close, which will take care of
        # shutting down the service's connections and such. So the only thing
        # we really need to do here is delete the service from the local_service
        # map, which will cause self.local_service_changed to unpublish the
        # service and remove it from the introspection service.
        del self.local_services[service.id]
    
    @synchronized_on("lock")
    def setup_inbound_socket(self, socket):
        # Create a connection and then add it to our list of connections
        connection = local.RemoteConnection(self, socket)
        self.bound_connections.add(connection)
    
    def connect(self, host, port, service_id, timeout=10, open_listener=None,
                close_listener=None, fail_listener=None, lock=None):
        """
        Opens a connection to the specified service on the specified host/port.
        
        The connection will be returned immediately. The actual connection to
        the server will be made as soon as possible in the future. If you need
        to block until the connection actually connects, call wait_for_connect
        on the returned Connection object.
        
        The connection will attempt to reconnect indefinitely whenever it is
        disconnected. If you don't want this behavior, specify a close_listener
        that calls the connection's close method.
        
        Timeout is the TCP timeout to use when connecting. The default is 10;
        this is usually a suitable default. You'll probably only want to
        increase this if you're working on a particularly latent network.
        
        open_listener and close_listener are functions accepting one argument.
        They will be called when the connection successfully connects and when
        the connection disconnects, respectively, and the connection itself
        will be passed in. They are both run synchronously on the connection's
        input thread, so it's guaranteed that, for example, the connection will
        not attempt to reconnect until close_listener has returned. Thus
        close_listener could be set to a function that just closes the
        specified connection in order to effectively disable the auto-reconnect
        feature of connections.
        """
        return remote.Connection(self, host, port, service_id, timeout, open_listener, close_listener, fail_listener, lock)
    
    def connect_to(self, info_filter, timeout=10, open_listener=None, close_listener=None, fail_listener=None, lock=None):
        """
        Locates the first service in the list of discovered services and uses
        self.connect to connect to it. The connection is then returned.
        
        This function will be going away soon. Service proxies (which can be
        obtained using self.get_service_proxy) are the replacement; a single
        service proxy is quite similar to this method, but it can follow the
        service across restarts of the underlying process publishing the
        service, which this method can't.
        """
        with self.lock:
            for service_id, d in self.discovered_services.items():
                if filter_matches(d.info, info_filter):
                    host, port = d.locations.keys()[0]
                    return self.connect(host, port, service_id, timeout, open_listener, close_listener, fail_listener, lock)
            raise exceptions.NoMatchingServiceException()
    
    def get_service_proxy(self, info_filter, bind_function=None, unbind_function=None, multiple=False):
        """
        Returns a service proxy that will connect to services matching the
        specified info object filter. If multiple is False (the default), a
        single service proxy will be returned. If multiple is True, a multiple
        service proxy will be returned. See proxy.SingleServiceProxy and
        proxy.MultipleServiceProxy for the differences between the two.
        
        bind_function and unbind_function are optional functions that will be
        called when the proxy binds to and unbinds from a service,
        respectively. Binding is where a proxy discovers a new service matching
        its info filter and establishes a connection to it. Unbinding is where
        the proxy disconnects from said connection, usually because the service
        went away.
        """
        with self.lock:
            if multiple:
                return proxy.MultipleServiceProxy(self, info_filter, bind_function, unbind_function)
            else:
                return proxy.SingleServiceProxy(self, info_filter)
    
    @synchronized_on("lock")
    def close(self):
        """
        Closes this bus and all services registered on it.
        """
        if self.closed: # Already closed
            return
        self.closed = True
        # First we shut down all of our discoverers
        for discoverer in self.discoverers:
            discoverer.shutdown()
        # Then we need to close all of our services. Closing a service causes
        # self._close_service to be called, which removes the service from the
        # list of services, which causes self.local_service_changed to be
        # called, which unpublishes the service. So we don't need to worry
        # about unpublishing services aside from this.
        for service_id in list(self.local_services):
            self.local_services[service_id].close()
        # Then we shut down all of the publishers
        for publisher in self.publishers:
            publisher.shutdown()
        # Then we shut down the server socket
        net.shutdown(self.server)
        # Then we close all of the connections currently connected to us
        for c in self.bound_connections:
            with no_exceptions:
                c.close()
        # And that's it!
    
    @synchronized_on("lock")
    def install_publisher(self, publisher):
        # Add the publisher to our list and start it up
        self.publishers.add(publisher)
        publisher.startup(self)
        # Then register all of our local services with the publisher
        for service in self.local_services.values():
            publisher.add(service)
    
    @synchronized_on("lock")
    def remove_publisher(self, publisher):
        # Check to make sure that the publisher is already installed
        if publisher not in self.publishers:
            # TODO: Not sure why we're using __builtin__ here...
            raise __builtin__.ValueError("The specified publisher is not currently installed on this bus.")
        # Remove the publisher from our list of publishers
        self.publishers.remove(publisher)
        # Unpublish all of our services from the publisher
        for service in self.local_services.values():
            if service.active:
                publisher.remove(service)
        # Then we shut down the publisher
        publisher.shutdown()
    
    @synchronized_on("lock")
    def install_discoverer(self, discoverer):
        # Add the discoverer to our list of discoverers, then start it up
        self.discoverers.add(discoverer)
        discoverer.startup(self)
    
    @synchronized_on("lock")
    def remove_discoverer(self, discoverer):
        # Check to make sure that the discoverer has already been installed
        if discoverer not in self.discoverers:
            # TODO: Ditto from remove_publisher
            raise __builtin__.ValueError("The specified discoverer is not currently installed on this bus.")
        # Remove the discoverer from our list of discoverers, then shut it
        # down
        self.discoverers.remove(discoverer)
        discoverer.shutdown()
    
    def set_local_info_builtins(self, host, port, service_id, info):
        new_info = info.copy()
        new_info["host"] = host
        new_info["port"] = port
        new_info["service"] = service_id
        return new_info
    
    def set_remote_info_builtins(self, service_id, info):
        """
        Adds some values to the specified info object. The only one added right
        now is hostname, which is the value of socket.gethostname(). I haven't
        really standardized the list of values added here; I hope to at some
        point, though, and have all Autobus client libraries add the same ones.
        """
        info["hostname"] = gethostname()
    
    @synchronized_on("lock")
    def discover(self, discoverer, host, port, service_id, info):
        # print "Discovered:", (host, port, service_id, info)
        # Add the relevant local builtins
        info = self.set_local_info_builtins(host, port, service_id, info)
        # Check to see if the specified service has been discovered yet, and if
        # it hasn't, create an entry for it
        is_new_service = False
        if service_id not in self.discovered_services:
            self.discovered_services[service_id] = DiscoveredService(info)
            is_new_service = True
        discovered_service = self.discovered_services[service_id]
        # Check to see if the specified host/port combination is already
        # present, and if it isn't, add it.
        if (host, port) not in discovered_service.locations:
            discovered_service.locations[(host, port)] = []
        discoverer_list = discovered_service.locations[(host, port)]
        # Check to see if this discoverer has already discovered that host/port
        if discoverer in discoverer_list:
            print ("Warning: discoverer " + str(discoverer) + 
                   " tried to rediscover " + str((host, port, service_id)) +
                   " with info " + str(info))
            return
        # It hasn't, so add it.
        discoverer_list.append(discoverer)
        # The check to see if we need to notify listeners, and do so if we
        # need to
        if is_new_service:
            self.notify_service_listeners(service_id, host, port, info, DISCOVERED) 
    
    @synchronized_on("lock")
    def undiscover(self, discoverer, host, port, service_id):
        # print "Undiscovered:", (host, port, service_id)
        # Check to see if the specified service has been discovered.
        if service_id not in self.discovered_services:
            print ("Warning: discoverer " + str(discoverer) + " tried to "
                   "undiscover " + str((host, port, service_id)) + " when "
                   "such a service does not exist.")
            return
        discovered_service = self.discovered_services[service_id]
        if (host, port) not in discovered_service.locations:
            print ("Warning: discoverer " + str(discoverer) + " tried to "
                   "undiscover " + str((host, port, service_id)) + " when "
                   "that host/port has not yet been discovered.")
            return
        discoverer_list = discovered_service.locations[(host, port)]
        if discoverer not in discoverer_list:
            print ("Warning: discoverer " + str(discoverer) + " tried to "
                   "undiscover " + str((host, port, service_id)) + " when "
                   "this discoverer hasn't discovered that host/port yet.")
            return
        discoverer_list.remove(discoverer)
        if not discoverer_list:
            if discovered_service.locations.keys()[0] == (host, port): # We're
                # removing the first (and therefore default) location, so if
                # there's another location, we need to let the service
                # listeners know that there's a new default location
                if len(discovered_service.locations) > 1: # There will be
                    # another location even after we delete this one
                    new_host, new_port = discovered_service.locations.keys()[1]
                    if not self.closed: # Don't issue changes if we're shutting down
                        self.notify_service_listeners(service_id, new_host, new_port, discovered_service.info, CHANGED)
            del discovered_service.locations[(host, port)]
            if not discovered_service.locations: # That was the last location
                # available for this service, so we delete the service itself,
                # and notify listeners that it was deleted
                del self.discovered_services[service_id]
                self.notify_service_listeners(service_id, host, port, discovered_service.info, UNDISCOVERED)
    
    @synchronized_on("lock")
    def add_service_listener(self, listener, info_filter=None, initial=False):
        """
        Listens for changes in services that are available. listener is a
        function listener(service_id, host, port, info, event) which will be
        called whenever a service becomes available, a service disappears, or
        the host/port that should be used to access a particular service
        changes. service_id is the id of the service; host/port is the host/port
        at which the service can be found, info is the service's info object,
        and event is one of DISCOVERED, UNDISCOVERED, or CHANGED.
        
        If info_filter is a dictionary, only services with info objects matching
        that particular filter (as per the filter_matches function) will cause
        the listener to be called. If info_filter is None (the default), or the
        empty dictionary (since all info objects match the empty dictionary),
        the listener will be called for all services.
        
        If initial is True, the listener will be immediately (and synchronously)
        called once for each service that already exists, passing in DISCOVERED
        as the event. Otherwise, the listener will only be called once the next
        
        """
        # Add the listener to our list of listeners
        self.service_listeners.append((info_filter, listener))
        # Check to see if we're supposed to notify the listener about all
        # matching services that already exist
        if initial:
            # Scan all of the services
            for service_id, discovered_service in self.discovered_services.items():
                if filter_matches(discovered_service.info, info_filter):
                    # If this service matches, notify the listener about it
                    host, port = discovered_service.locations.keys()[0]
                    with print_exceptions:
                        listener(service_id, host, port, discovered_service.info, DISCOVERED)
    
    @synchronized_on("lock")
    def remove_service_listener(self, listener, initial=False):
        # Scan the list of listeners and remove this one. Inefficient, it's
        # true, and I hope to make it more efficient later on.
        for index, (info_filter, l) in enumerate(self.service_listeners[:]):
            # See if we've hit the right listener
            if l == listener:
                # If we have, remove the listener
                del self.service_listeners[index]
                if initial:
                    # Scan through the list of services
                    for service_id, discovered_service in self.discovered_services.items():
                        if filter_matches(discovered_service.info, info_filter):
                            # This service matched, so we notify this
                            # listener that the service was removed
                            with print_exceptions:
                                listener(service_id, None, None, None, UNDISCOVERED)
                # We've found our listener and deleted it, so we return now
                return
    
    def notify_service_listeners(self, service_id, host, port, info, event):
        for filter, listener in self.service_listeners:
            if filter_matches(info, filter):
                with print_exceptions:
                    listener(service_id, host, port, info, event)
    
    def local_service_changed(self, service_id, old, new):
        """
        Called (by the local_services property table) when services come and go.
        All we really need to do is publish/unpublish the service.
        """
        if old:
            for publisher in self.publishers:
                publisher.remove(old)
        if new:
            for publisher in self.publishers:
                publisher.add(new)
Exemplo n.º 9
0
def RECIVE_TEXTMSG(CLIENT: socket.socket) -> str:
    global SERVICE, FORMAT, HEADER_LENGTH, MAX_CON_RETRY
    FINAL = None
    MESSAGE_CONFIRM = False
    CONN_RETRY = 0
    CLIENT.settimeout(5)
    while not MESSAGE_CONFIRM and SERVICE:
        try:
            MESSAGE_LEN = CLIENT.recv(HEADER_LENGTH)
            if MESSAGE_LEN:
                CAPTURE_LEN = int(MESSAGE_LEN.decode(FORMAT))
                MESSAGE = CLIENT.recv(CAPTURE_LEN)
                FINAL = MESSAGE.decode(FORMAT)
                MESSAGE_CONFIRM = True
        except ConnectionResetError:
            MESSAGE_CONFIRM = True
        except ConnectionAbortedError:
            MESSAGE_CONFIRM = True
        except socket.timeout:
            CONN_RETRY += 1
            if not SERVICE:
                MESSAGE_CONFIRM = True
            if CONN_RETRY > MAX_CON_RETRY:
                MESSAGE_CONFIRM = True
    CLIENT.settimeout(None)
    return FINAL
Exemplo n.º 10
0
def receive(conn: socket.socket) -> bytearray:
    """Read raw bytes from a client socket.
    """
    conn.settimeout(REQUEST_SOCKET_TIMEOUT)

    try:
        received = bytearray()

        while True:
            if len(received) > REQUEST_MAX_SIZE:
                break

            if b"\r\n\r\n" in received:
                break

            chunk = conn.recv(REQUEST_CHUNK_SIZE)
            if not chunk:
                break

            received += chunk

    except socket.timeout:
        raise HTTPException(HTTPStatus.REQUEST_TIMEOUT)

    return received
Exemplo n.º 11
0
    def _echo_loop(self, connection: socket.socket) -> None:
        """
        Get a connection and echo all the data received on it back (worker thread target).
        :param connection: Socket to communicate through with the client
        :return: None
        """
        # Set socket to timeout to allow checking the alive flag
        connection.settimeout(0.1)

        # Loop for messages to send back
        while self.isAlive.isSet():
            try:
                # Fail fast
                data = connection.recv(256)
            except socket.timeout:
                # No message available, retry if still alive
                continue
            except socket.error as e:
                # Different exception: Shutdown connection
                print(e)
                connection.close()
            else:
                if data:
                    # Hook for resolving response
                    echo_msg = self.determine_response(data)
                    # Echo the message and wait for more if still alive
                    connection.sendall(echo_msg)
                else:
                    # Data is empty and there will be no future messages
                    break

        # Shutdown connection properly
        connection.close()
Exemplo n.º 12
0
 def _receive_from(self, conn: socket.socket, address, chunk_size=2048):
     buffer = b''
     # read from socket buffer
     try:
         # set timeout
         conn.settimeout(self._timeout)
         # self._logger.print("[*] Start receive from %s:%d" % address)
         while 1:
             data = conn.recv(chunk_size)
             if not data:
                 break
             buffer += data
     except KeyboardInterrupt:
         self._logger.print_err("[!!] Keyboard interrupt. Exit...")
         close_socket_pass_exc(conn)
         close_socket_pass_exc(self._server_socket)
         exit()
     except Exception as e:
         if bypass_error(e):
             return buffer
         out = format_exc()
         out += '[!!] Fail receive data from %s:%d\n' % address
         out += '[!!] Caught an exception on %s: %s\n' % (self.name, str(e))
         self._logger.print_err(out)
         # if endpoint is disconnected return false
         if len(e.args) >= 2 and e.args[1] == 'Transport endpoint is not connected':
             return False
     return buffer
Exemplo n.º 13
0
def send_data(data_bytes: bytes,
              protocol_type: ProtocolType,
              sock: socket.socket,
              timeout=0.1):
    if protocol_type == ProtocolType.LD382A or protocol_type == ProtocolType.LD686:
        data_bytes = bytes([*data_bytes, 0x0F])

    checksum = checksum_for(data_bytes)

    total_bytes = bytes([*data_bytes, checksum])

    print(f"Send: '{data_bytes.hex()}' CS:{hex(checksum)}")
    sock.sendall(total_bytes)

    if timeout > 0:
        sock.settimeout(timeout)
        response = []
        while True:
            try:
                r = sock.recv(1)
                response.append(r[0])
            except socket.timeout:
                break
        response = bytes(response)
        response_data = response[:-1]
        expected_checksum = checksum_for(response_data)
        actual_checksum = response[-1]
        print(
            f" -> Receive: '{response_data.hex()}' CS:{hex(actual_checksum)}")
        if actual_checksum != expected_checksum:
            raise RuntimeWarning(
                f"Received invalid checksum: {actual_checksum}!={expected_checksum}"
            )
        return response_data
    return None
Exemplo n.º 14
0
    def __send_to_target(self, next_stop: Packet, slave_client: socket.socket):
        try:
            a = next_stop.get(
                Packet.Options.REQUEST).encode().decode("unicode_escape")
            next_stop.set(a, Packet.Options.REQUEST)

            #if the target doesn't reply back, aka google.com:80
            slave_client.settimeout(120)

            slave_client.connect((next_stop.get(Packet.Options.IP),
                                  next_stop.get(Packet.Options.PORT)))
            slave_client.sendall(
                (next_stop.get(Packet.Options.REQUEST)).encode())

            try:
                reply = slave_client.recv(65536).decode()

            except:
                reply = "Error 502: Destination has not response back"

        except:
            reply = "Error 0xb042f: Destination is unvailable"

        slave_client.close()

        return reply
Exemplo n.º 15
0
 def receive_data(from_socket: socket.socket, from_timeout=2) -> bytes:
     """
     Centralised fuction to handle receiving one or more packet buffers from TCP socket
     Args:
         from_socket:
             Socket sending stream to this instance.
         from_timeout:
             Set timeout for from_socket
     Returns:
             Complete binary stream from socket
     """
     from_socket.settimeout(from_timeout)
     fragments: List[bytes] = []
     try:
         stream = from_socket.recv(4096)
         fragments.append(stream)
         while True:
             if len(stream) < 4096:
                 break
             else:
                 stream = from_socket.recv(4096)
                 fragments.append(stream)
     except TimeoutError:
         pass
     return b''.join(fragments)
Exemplo n.º 16
0
def receive_message(sock: socket.socket,
                    timeout: Optional[float] = None) -> Optional[bytearray]:
    """
    :param sock:
    :param timeout: Timeout until the first byte, NOT THE WHOLE MESSAGE.
    :return:
    """
    assert timeout is None or timeout > 0

    if timeout is None:
        type_header = recv_all(sock, 1)
    else:
        sock.settimeout(timeout)
        try:
            type_header = recv_all(sock, 1)
            print("wow, ", type_header)
        except socket.timeout:
            print("TIMEOUT!")
            return None
        finally:
            sock.settimeout(None)  # put it back in blocking mode

    length_header = recv_all(sock, 4)
    length = int.from_bytes(length_header, byteorder="big")
    msg = recv_all(sock, length)

    return msg
Exemplo n.º 17
0
    def __demodulate(self, connection: socket.socket):
        connection.settimeout(self.TIMEOUT)
        time.sleep(self.TIMEOUT)

        total_data = []
        while True:
            try:
                data = connection.recv(65536)
                if data:
                    total_data.append(data)
                else:
                    break
            except socket.timeout:
                break

        if len(total_data) == 0:
            logger.error("Did not receive any data from socket.")

        arr = IQArray(
            np.array(np.frombuffer(b"".join(total_data), dtype=np.complex64)))
        signal = Signal("", "")
        signal.iq_array = arr
        pa = ProtocolAnalyzer(signal)
        pa.get_protocol_from_signal()
        return pa.plain_bits_str
Exemplo n.º 18
0
    def wrapSSL(self, sock: socket.socket, **kwargs: Any) -> ssl.SSLSocket:
        """
		Wraps a socket in an SSL context.

		Args:
			sock: The unencrypted socket.
			**kwargs: Key-word only arguments to be passed to the [`SSLContext.wrap_socket`][1] method.
				[1]: <https://docs.python.org/library/ssl.html#ssl.SSLContext.wrap_socket>

		Returns:
			The socket wrapped in an
			[SSL context.](https://docs.python.org/library/ssl.html#ssl.SSLContext)
		"""
        kwargs["do_handshake_on_connect"] = False  # Avoid race condition.
        with self._recv_lock, self._send_lock:
            originalTimeout: Union[float, None] = sock.gettimeout()
            sock.settimeout(None)
            try:
                context: ssl.SSLContext = ssl.SSLContext(
                    ssl.PROTOCOL_TLS_CLIENT)
                context.load_verify_locations(CERT_LOCATION)
                sock = context.wrap_socket(sock, **kwargs)
                self.doSSLHandshake(sock)
            finally:
                sock.settimeout(originalTimeout)
        return sock
Exemplo n.º 19
0
def run(client_socket: socket.socket, address: (str, int), settings: Settings) -> None:
    """
    Thread which handles one client connection
    """
    logging.info(f'Client (IP: {address[0]}) has connected')
    client_socket.settimeout(settings.socket_timeout)
    wsock = SocketWrapper(client_socket)

    try:
        # now send timestamp and start signal
        logging.info('Starting to send data')
        wsock.send_message(bytes(TimeMessage()))
        wsock.send_message(bytes(CommandMessage(Command.COMMAND)))
        wsock.send_message(bytes(StringMessage(settings.airodump_command)))
        wsock.send_message(bytes(CommandMessage(Command.START)))

        msg = wsock.receive_message()
        if isinstance(msg, CommandMessage):
            if msg.command == Command.CYA:
                logging.info('Client has signaled successfull start of operation')
                # remote device has started successfully

        client_socket.close()
    except ConnectionResetError:
        logging.info(f'Client (IP: {address[0]}) has reset the connection')
        client_socket.close()
Exemplo n.º 20
0
    def send_data(to_socket: socket.socket,
                  data_stream: bytes,
                  send_timeout=2) -> None:
        """
        Centralised function to handle sending data stream to receive data. Sends data in consistent
        buffer sizes

        Args:
            to_socket:
                Socket to send stream to
            data_stream:
                Data stream to send
            send_timeout:
                Set timeout for to_socket
        """
        to_socket.settimeout(send_timeout)
        try:
            data_fragments = []
            for i in range(0, len(data_stream), 4096):
                # Break data stream into byte sized bites
                data_fragments.append(data_stream[i:i + 4096])
            if data_fragments[-1] == 4096:
                # Make sure last fragment isn't BUFFER bytes long
                data_fragments.append(b'\n')
            for frag in data_fragments:
                to_socket.send(frag)
        except TimeoutError:
            pass
Exemplo n.º 21
0
def send_task(conn: socket.socket, q: Queue, stop_e: threading.Event, delimiter: bytes, timeout=None):

    print("[SEND][INFO] Started")
    conn.settimeout(timeout)

    try:
        while True:
            try:
                n = q.get(timeout=timeout)
                q.task_done()
            except Empty:
                if stop_e.is_set():
                    print(SharedData.bold("[SEND][INFO] Event set!"))
                    return
            else:
                try:
                    tcp_send(n, conn, delimiter)

                except socket.timeout:
                    # really just want to use logging and dump logs in other thread..
                    print(SharedData.red("[Send][CRIT] Connection Broken!"))
                    break
    except Exception:
        print(SharedData.bold("[SEND][CRIT] Stopping SEND!"))
        stop_e.set()
        raise
Exemplo n.º 22
0
def send_and_receive_handshake(clientsocket: socket.socket):
    Keys.CLIENT_PRIV_KEY = RSA.generate(bits=1024)
    Keys.CLIENT_PUB_KEY = Keys.CLIENT_PRIV_KEY.publickey()

    pub_key_payload = Keys.CLIENT_PUB_KEY.exportKey()
    buf = io.BytesIO()
    buf.write(int(0).to_bytes(4, byteorder="big"))
    buf.write(len(pub_key_payload).to_bytes(4, byteorder="big"))
    buf.write(pub_key_payload)

    buf.seek(0)
    clientsocket.sendall(buf.read())

    buf = io.BytesIO()
    clientsocket.settimeout(5)
    while True:
        data = clientsocket.recv(1024)
        if not data:
            break
        buf.write(data)

    buf.seek(0)
    opcode = int.from_bytes(buf.read(4), byteorder="big")
    if opcode != 0:
        # print("invalid opcode", opcode, "expected", 0)
        return

    payload_length = int.from_bytes(buf.read(4), byteorder="big")
    payload = buf.read(payload_length)
    Keys.SERVER_PUB_KEY = RSA.importKey(payload)
Exemplo n.º 23
0
def send_and_receive_part(clientsocket: socket.socket, index: int):
    part = FLAG_PARTS[index]
    encrypted, = Keys.SERVER_PUB_KEY.encrypt(part.encode("utf-8"), 32)

    buf = io.BytesIO()
    buf.write(int(index + 1).to_bytes(4, byteorder="big"))
    buf.write(len(encrypted).to_bytes(4, byteorder="big"))
    buf.write(encrypted)

    buf.seek(0)
    clientsocket.sendall(buf.read())

    buf = io.BytesIO()
    clientsocket.settimeout(5)
    while True:
        data = clientsocket.recv(1024)
        if not data:
            break
        buf.write(data)

    buf.seek(0)
    opcode = int.from_bytes(buf.read(4), byteorder="big")
    if opcode != index + 1:
        # print("invalid opcode", opcode, "expected", index + 1)
        raise Exception()

    payload_length = int.from_bytes(buf.read(4), byteorder="big")
    payload = buf.read(payload_length)
    decrypted = Keys.CLIENT_PRIV_KEY.decrypt(payload)
    if decrypted.decode("utf-8") != part:
        # print(decrypted.decode("utf-8"), "did not match part", index, part)
        raise Exception()
Exemplo n.º 24
0
    def serve(self, connection: socket.socket, addr):

        # print("Serving on port: " + str(self.server_port))
        #Peers may close a connection if they receive no messages for a certain period of time.
        #Two minutes is the default value for timeout
        connection.settimeout(120)

        infohash = ""

        try:
            msg = connection.recv(1024)
        except:
            return

        if not msg:
            return

        msg = json.loads(msg)

        if msg["message"] == "handshake":
            # print(msg)
            # print(f"Handshake from {addr}")
            #If the client doesn't contain the file, then it closes the connection with the remote peer
            # print(f"Files: {self.files}")
            if msg["infohash"] not in self.files:

                # print("Closing the conection...")
                connection.close()
                return

            # print("correct infohash")
            #If the peer contains the file, then sends a handshake msg to the remote peer
            infohash = msg["infohash"]
            handshake(connection, msg["infohash"], self.id)
            # print(f"handshake answer sended to {addr}")

        elif msg["message"] == "keep-alive":
            return

        elif msg["message"] == "interested":
            peer_interested = True

        elif msg["message"] == "not interested":
            peer_interested = False

        elif msg["message"] == "have":
            return

        elif msg["message"] == "bitfield":
            # print(f"Bitfield from {addr}")
            bitfield_answer(connection,
                            self.files[msg['infohash']]["bitfield"])

        elif msg["message"] == "piece":
            with open(self.files[msg["infohash"]]["path"], "rb") as f:
                f.seek(msg['index'] * msg['length'])
                chunk = f.read(msg['length'])
                data(connection, chunk)
        connection.close()
        threading.current_thread()._delete()
Exemplo n.º 25
0
def send(sock: socket.socket, data: bytes):
    """
    Implementation of the sending logic for sending data over a slow,
    lossy, constrained network.

    Args:
        sock -- A socket object, constructed and initialized to communicate
                over a simulated lossy network.
        data -- A bytes object, containing the data to send over the network.
    """

    # determines if the last packet sent was successfully transmitted
    global SUCCESS, EXPECTED_SEQUENCE, TIMEOUT

    # Naive implementation where we chunk the data to be sent into
    # packets as large as the network will allow, and then send them
    # over the network, pausing half a second between sends to let the
    # network "rest" :)
    logger = homework5.logging.get_logger("hw5-sender")

    # Let chunk size be the length of the message minus the header
    # and some padding to fit the of the sequence numer
    chunk_size = homework5.MAX_PACKET - 4

    # let the pause value be determined by the timeout value
    pause = TIMEOUT

    offsets = range(0, len(data), chunk_size)
    for chunk in [data[i:i + chunk_size] for i in offsets]:
        # Assume a chunk transmission failed until the ack proves
        # it wrong
        SUCCESS = False

        reset_timer()
        # keep sending until the packet has been successfully acknowledged
        sock.send(make_packet(EXPECTED_SEQUENCE, chunk))

        # set teh timeout for a receive of an Ack
        sock.settimeout(TIMEOUT)

        while True:
            # Try to fetch the data from the acknowledgement packet
            try:
                # Fetch the acknowledge packet
                data = sock.recv(homework5.MAX_PACKET)
                sequence_num = decode_packet(data)[0]

                # If the acknowledgement packet corresponds as
                # Expected,  continue with the next
                if sequence_num is EXPECTED_SEQUENCE:
                    update_timeout()
                    update_sequence()
                    break

            # If the ack packet never arrived, resend the whole data packet
            except socket.timeout:
                sock.send(make_packet(EXPECTED_SEQUENCE, chunk))

        pause = TIMEOUT
Exemplo n.º 26
0
 def __init__(self,
              sock: socket.socket,
              buffer_size: int = 256,
              timeout: int = SOCKET_TIMEOUT_SECS):
     self._socket = sock
     self._buffer_size = buffer_size
     self._buffer = b''
     sock.settimeout(timeout)
Exemplo n.º 27
0
def _set_timeout(sock: socket.socket, timeout: float):
    """Set timeout of the socket and restore."""
    old_timeout = sock.gettimeout()
    sock.settimeout(timeout)
    try:
        yield
    finally:
        # restore timeout anyway.
        sock.settimeout(old_timeout)
    return
Exemplo n.º 28
0
 def serve_client(self, client_sock: socket.socket):
     client_sock.settimeout(TIMEOUT)
     while True:
         data = client_sock.recv(1024)
         if data == b"":
             client_sock.shutdown(1)
             client_sock.close()
             break
         if data:
             client_sock.sendall(data)
Exemplo n.º 29
0
 def __init__(self, sock: socket.socket, chunk_size=2**18, timeout=10):
     sock.settimeout(0)
     self._sock = sock
     self.chunk_size = chunk_size
     self._recv_buffer = b''
     self._send_buffer = deque()
     self._closing = False
     self.timeout = timeout
     self._timeout_time = time.time() + timeout
     Loop.ACTIVE.enqueue(self._loop())
Exemplo n.º 30
0
def send(sock: socket.socket, data: bytes):
    """
    Implementation of the sending logic for sending data over a slow,
    lossy, constrained network.

    Args:
        sock -- A socket object, constructed and initialized to communicate
                over a simulated lossy network.
        data -- A bytes object, containing the data to send over the network.
    """

    # Naive implementation where we chunk the data to be sent into
    # packets as large as the network will allow, and then send them
    # over the network, pausing half a second between sends to let the
    # network "rest" :)

    timeoutInterval = 1.0
    headerSize = 4
    logger = homework5.logging.get_logger("hw5-sender")
    chunk_size = homework5.MAX_PACKET - headerSize
    packetCount = 1
    pause = 2.0
    avgRTT = 0
    lastRTT = 0
    tripCount = 1
    sequenceNumber = 0
    offsets = range(0, len(data), chunk_size)
    for chunk in [data[i:i + chunk_size] for i in offsets]:
        sequenceNumber = packetCount * chunk_size
        packetCount = packetCount + 1
        chunk = struct.pack("i", sequenceNumber) + chunk
        sock.send(chunk)
        start = time.time()
        logger.info("Pausing for %f seconds", round(pause, 2))
        if packetCount != 2:
            pause = computeTimeout(avgRTT, lastRTT)
        sock.settimeout(pause)

        while True:
            try:
                data = sock.recv(headerSize)
                tempSequenceNumber = struct.unpack("i", data[:4])[0]
                if tempSequenceNumber == sequenceNumber:
                    lastRTT = time.time() - start
                    avgRTT = ((
                        (tripCount - 1) * avgRTT) + lastRTT) / (tripCount)
                    tripCount = tripCount + 1
                    break
            except:
                pause = computeTimeout((avgRTT + 1.0), pause)
                sock.send(chunk)
                start = time.time()
                if packetCount == 2:
                    pause = 2.0
                sock.settimeout(pause)
Exemplo n.º 31
0
def send(sock: socket.socket, command):
    sock.sendall(f'{command}\n'.encode())
    sock.settimeout(0.5)
    res = ''
    try:
        mess = sock.recv(BUFFER_SIZE).decode()
        while mess:
            res += mess
            mess = sock.recv(BUFFER_SIZE).decode()
    finally:
        return res
Exemplo n.º 32
0
    def _authenticate(
        self,
        receive_frame_generator: Iterator[Optional[bytes]],
        tcp_socket: socket.socket
    ) -> None:
        """ Handles authentication challenge. """

        # Set timeout on socket, after which, if AuthenticationChallengeFrame is not received,
        # SigningService will have to reconnect.
        tcp_socket.settimeout(RECEIVE_AUTHENTICATION_CHALLENGE_TIMEOUT)

        # After establishing a TCP connection start listening for the AuthenticationChallengeFrame.
        try:
            raw_message_received = next(receive_frame_generator)
            authentication_challenge_frame = AbstractFrame.deserialize(
                raw_message_received,
                public_key=self.concent_public_key,
            )
            # If SigningService receive any other message that AuthenticationChallengeFrame
            # disconnect, log the incident and treat it as a failure to connect.
            if not isinstance(authentication_challenge_frame, AuthenticationChallengeFrame):
                logger.info(
                    f'SigningService received {type(authentication_challenge_frame)} instead of AuthenticationChallengeFrame.'
                )
                raise socket.error()
        # If received message is invalid or
        # if nothing was received in a predefined time,
        # disconnect, log the incident and treat it as a failure to connect.
        except (MiddlemanProtocolError, socket.timeout) as exception:
            logger.info(f'SigningService failed to receive AuthenticationChallengeFrame with exception: {exception}.')
            raise socket.error()

        # If you receive a valid challenge, sign it with the private key of the service and
        # send AuthenticationResponseFrame with signature as payload.
        authentication_response_frame = AuthenticationResponseFrame(
            payload=self._get_authentication_challenge_signature(
                authentication_challenge_frame.payload,
            ),
            request_id=authentication_challenge_frame.request_id,
        )

        try:
            send_over_stream(
                connection=tcp_socket,
                raw_message=authentication_response_frame,
                private_key=self.signing_service_private_key,
            )
        # If the server disconnects, log the incident and treat it as a failure to connect.
        except socket.error as exception:
            logger.info(
                f'MiddleMan server disconnects after receiving AuthenticationResponseFrame with exception: {exception}.'
            )
            raise socket.error()
        logger.info('Authentication successful. ')
Exemplo n.º 33
0
def handle_clientsoc(client_soc: socket.socket, context: dict,
                     cir: circuit.Circuit, flag):
    """
    Main Handler function for onion proxy, it sends relay cells with the client data to and from the server
    :param client_soc: the client socket connection
    :param context: a dict of socket connections to our guard node
    :param cir: the created circuit object
    :return: None
    """
    # Set the client socket timeout to 2 seconds, and initialize data variable
    client_soc.settimeout(2.0)
    data = b''
    # cir.context['ssl_socket'].setblocking(0)
    # Receive Data from client. If client is done sending data, socket will either timeout, or receive will be none
    try:
        while True:
            # Use Socket.Recv function from socket library
            receive = client_soc.recv(1024)
            data += receive
            if not receive:
                break
    except socket.timeout:
        pass

    # # Little snippet of code for http delete later
    if not flag:
        while b'iplocation' not in data:
            client_soc = connect.listen_for_connection(context)
            client_soc.settimeout(1.0)
            try:
                while True:
                    data = client_soc.recv(1024)
                    if not data:
                        break
            except socket.timeout:
                pass

    # end snippet

    print("[*] Data Received from Client:{}".format(data))
    if data:
        flag = True
        # Determine the connection type - for example get, post, connect ...
        connection_method = determine_connnection(data)
        if connection_method is not None:
            # If all is good handle the request from the client
            handle_request(connection_method, client_soc, cir, data)
            return flag
        # If we cant recognize the http command close the connection
        else:
            print(
                "[*] Unrecognized Command from Client...\n[*] Connection Closing..."
            )
            client_soc.close()
Exemplo n.º 34
0
def server_init(sock: socket.socket, address: tuple, timeout):
    sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    sock.settimeout(timeout)
    try:
        sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)
    except AttributeError:
        pass
    except socket.error as e:
        if e.errno != errno.ENOPROTOOPT:
            raise
    sock.bind(address)
Exemplo n.º 35
0
    def _process_client(self, client_sock: socket.socket, client_num: int):
        """client_sock: socket accepted from the client"""
        """Program must recieve the list of files from the client,
        decide which filed to send, pack them to archive and send it.
        """
        client_sock.settimeout(5)
        data: bytes = b""
        while True:
            received = client_sock.recv(1024)
            logging.debug("Data got: {}".format(received))
            if not received:
                break
            else:
                data += received
            if data[-4:] == SIGTTERM:
                data = data[:-4]
                break
        parts = data.split(SIGTPTERM)
        if len(parts) > 3:
            raise Exception("Mistake in protocol, now you must fix it")
        existed_files_list = json.loads(parts[0].decode("utf-8"))
        date: float = datetime.datetime.strptime(parts[1].decode("utf-8"), "%Y_%m_%d").timestamp()
        size = int(parts[2].decode("utf-8"))
        logging.debug("Retrieve this conditions:{} {}".format(parts[1].decode('utf-8'), size))

        to_send_list = []

        for file in self._get_files_in_folder():
            if file not in existed_files_list:
                file_path = self.concat_ff(self.folder_to_sync, file)
                info = os.stat(file_path)
                try:  # Should try to parse with custom parser
                    datetime_cond = self._parse_datetime(file) > date
                except:
                    datetime_cond = info.st_ctime > date
                if datetime_cond and info.st_size > size:
                    to_send_list.append(file)

        zip_name = "send_client{}.zip".format(client_num)
        with zipfile.ZipFile(zip_name, 'w') as zfd:
            for file in to_send_list:
                zfd.write(self.concat_ff(self.folder_to_sync, file), arcname=file)

        with open(zip_name, "rb") as fd:
            while True:
                data = fd.read(1024)
                if not data:
                    break
                client_sock.send(data)
        client_sock.close()

        os.remove(zip_name)
Exemplo n.º 36
0
def HandleWorkers(server: socket.socket, replay_memory: ReplayMemory,
                  mem_lock: Lock, param_queue: Queue, shutdown: Value):
    print("Listening for new workers...")
    server.settimeout(1)  # timeout period of 1 second

    num_workers = 0
    workers: Dict[int, socket.socket] = dict()
    state_dict = None

    while shutdown.value <= 0:
        try:
            worker, _ = server.accept()
            print("Connected to new worker")
            worker_id = num_workers
            worker_proc = Process(target=ReceivePlayouts,
                                  args=(worker, worker_id, replay_memory,
                                        mem_lock),
                                  daemon=True)
            worker_proc.start()

            if state_dict is not None:
                # Send the new worker the most up-to-date params
                buffer = io.BytesIO()
                torch.save(state_dict, buffer)
                param_bytes = buffer.getvalue()
                communication.Send(worker, buffer.getvalue())

            workers[worker_id] = worker
            num_workers += 1
        except socket.timeout:
            pass

        if not param_queue.empty():
            # Send the most up-to-date params to all the workers
            state_dict = None
            while not param_queue.empty():
                state_dict = param_queue.get()
            assert (state_dict is not None)

            buffer = io.BytesIO()
            torch.save(state_dict, buffer)
            param_bytes = buffer.getvalue()
            print("Sending new params to workers")
            for worker_id in workers.keys():
                worker: socket.socket = workers[worker_id]
                try:
                    communication.Send(worker, param_bytes)
                except:
                    # Something went wrong with this connection, so remove
                    # this worker
                    print(f"Error with worker {worker_id}, ending connection")
                    workers.pop(worker_id)
Exemplo n.º 37
0
def execute(client_socket: socket.socket):
    client_socket.settimeout(100.0)
    while True:
        try:
            command = client_socket.recv(100)
            result = subprocess.run(command,
                                    capture_output=True,
                                    encoding="UTF-8")
        except socket.timeout:
            break
        response = f"=== Execution Result: {str(result.returncode)} ===\n{result.stdout}=== Execution End ==="
        client_socket.send(bytes(response, "UTF-8"))
    client_socket.shutdown(socket.SHUT_RDWR)
Exemplo n.º 38
0
    def _live(me, s: socket.socket) -> (socket.socket, tuple):
        '''
        Listens on a server socket, when a client connects, returns a client socket.
        Check every second for a server stop request.
        '''
        s.settimeout(1.0)

        while me.alive:
            try:
                conn, addr = s.accept()
                yield conn, addr
            except socket.timeout as e:
                pass
Exemplo n.º 39
0
    def wait_connect(self):
        if self.needQuit:
            return
        self.init_graphic()
        self.informBoard = dashboard.InformBoard((10, 25), 15)

        test = 0
        server = Socket(socket.AF_INET, socket.SOCK_STREAM)
        if not test:
            while 1:
                try:
                    server.bind(("", config.Port))
                    break
                except socket.error as e:
                    self.inform(str(e))
                    for event in pygame.event.get():
                        if event.type == QUIT:
                            self.quit()
                            break
                    self.render_connection()
                    sleep(1)
            server.listen(2)
            server.settimeout(0.0)

            player1 = Player(1, server)
        else:
            player1 = HumanPlayer(1)

        if self.add_human:
            player2 = HumanPlayer(2)
            # add handler for human player
            self.pyeventHandlers.append(player2)
        else:
            player2 = Player(2, server)

        players = [player1, player2, None]

        timer = pygame.time.Clock()
        # wait until connected 2 players
        finished = 0
        player = players[finished]
        try:
            self.inform("waiting for AI to connect...")
            while finished < 2:
                for event in pygame.event.get():
                    if event.type == QUIT:
                        self.quit()
                        break
                if self.needQuit:
                    break
                if player.connect():
                    self.add_player(player)
                    self.inform("AI %s connected" % (player.name))
                    finished += 1
                    player = players[finished]
                self.render_connection()
                timer.tick(10)
        except KeyboardInterrupt:
            server.close()
            return False
        server.close()

        return True