gatenet.socket package

Submodules

gatenet.socket.base module

class gatenet.socket.base.BaseSocketServer(host='127.0.0.1', port=8000)[source]

Bases: ABC

Abstract base class for socket servers.

All socket server implementations (TCP, UDP, etc.) should inherit from this class and implement start and stop.

Example

>>> from gatenet.socket.tcp import TCPServer
>>> server = TCPServer(host="127.0.0.1", port=9000)
>>> server.start()
# Now connect with a TCP client to 127.0.0.1:9000
abstractmethod start()[source]

Start the server and begin handling incoming connections or data.

Example

>>> server = TCPServer(host="127.0.0.1", port=9000)
>>> server.start()
Raises:

NotImplementedError – If not implemented by a subclass.

abstractmethod stop()[source]

Stop the server and clean up resources.

Example

>>> server = TCPServer(host="127.0.0.1", port=9000)
>>> server.stop()
Raises:

NotImplementedError – If not implemented by a subclass.

gatenet.socket.tcp module

class gatenet.socket.tcp.TCPServer(host='0.0.0.0', port=8000)[source]

Bases: BaseSocketServer

Multithreaded TCP server that accepts incoming connections and echoes back any data it receives.

Each client connection is handled in a separate thread.

Example

>>> from gatenet.socket.tcp import TCPServer
>>> server = TCPServer(host="127.0.0.1", port=9000)
>>> server.start()
# Now connect with a TCP client to 127.0.0.1:9000
start()[source]

Start the TCP server, accept connections, and spawn a new thread for each client.

Example

>>> server = TCPServer(host="127.0.0.1", port=9000)
>>> server.start()
Raises:

OSError – If the socket is closed externally or binding fails.

stop()[source]

Stop the TCP server and release the socket.

Example

>>> server = TCPServer(host="127.0.0.1", port=9000)
>>> server.stop()

This method sets the running flag to False and closes the server socket.

gatenet.socket.udp module

class gatenet.socket.udp.UDPServer(host='0.0.0.0', port=8001)[source]

Bases: BaseSocketServer

UDP server that listens for datagrams and echoes them back with an ‘Echo: ‘ prefix.

Example

>>> from gatenet.socket.udp import UDPServer
>>> server = UDPServer(host="127.0.0.1", port=9001)
>>> server.start()
# Now send a UDP datagram to 127.0.0.1:9001
start()[source]

Start the UDP server and listen for incoming datagrams.

Example

>>> server = UDPServer(host="127.0.0.1", port=9001)
>>> server.start()
Raises:

OSError – If the socket is closed externally or binding fails.

stop()[source]

Stop the UDP server and close the socket.

Example

>>> server = UDPServer(host="127.0.0.1", port=9001)
>>> server.stop()

This method closes the server socket and prints a shutdown message.

Module contents

class gatenet.socket.BaseSocketServer(host='127.0.0.1', port=8000)[source]

Bases: ABC

Abstract base class for socket servers.

All socket server implementations (TCP, UDP, etc.) should inherit from this class and implement start and stop.

Example

>>> from gatenet.socket.tcp import TCPServer
>>> server = TCPServer(host="127.0.0.1", port=9000)
>>> server.start()
# Now connect with a TCP client to 127.0.0.1:9000
abstractmethod start()[source]

Start the server and begin handling incoming connections or data.

Example

>>> server = TCPServer(host="127.0.0.1", port=9000)
>>> server.start()
Raises:

NotImplementedError – If not implemented by a subclass.

abstractmethod stop()[source]

Stop the server and clean up resources.

Example

>>> server = TCPServer(host="127.0.0.1", port=9000)
>>> server.stop()
Raises:

NotImplementedError – If not implemented by a subclass.

class gatenet.socket.TCPServer(host='0.0.0.0', port=8000)[source]

Bases: BaseSocketServer

Multithreaded TCP server that accepts incoming connections and echoes back any data it receives.

Each client connection is handled in a separate thread.

Example

>>> from gatenet.socket.tcp import TCPServer
>>> server = TCPServer(host="127.0.0.1", port=9000)
>>> server.start()
# Now connect with a TCP client to 127.0.0.1:9000
start()[source]

Start the TCP server, accept connections, and spawn a new thread for each client.

Example

>>> server = TCPServer(host="127.0.0.1", port=9000)
>>> server.start()
Raises:

OSError – If the socket is closed externally or binding fails.

stop()[source]

Stop the TCP server and release the socket.

Example

>>> server = TCPServer(host="127.0.0.1", port=9000)
>>> server.stop()

This method sets the running flag to False and closes the server socket.

class gatenet.socket.UDPServer(host='0.0.0.0', port=8001)[source]

Bases: BaseSocketServer

UDP server that listens for datagrams and echoes them back with an ‘Echo: ‘ prefix.

Example

>>> from gatenet.socket.udp import UDPServer
>>> server = UDPServer(host="127.0.0.1", port=9001)
>>> server.start()
# Now send a UDP datagram to 127.0.0.1:9001
start()[source]

Start the UDP server and listen for incoming datagrams.

Example

>>> server = UDPServer(host="127.0.0.1", port=9001)
>>> server.start()
Raises:

OSError – If the socket is closed externally or binding fails.

stop()[source]

Stop the UDP server and close the socket.

Example

>>> server = UDPServer(host="127.0.0.1", port=9001)
>>> server.stop()

This method closes the server socket and prints a shutdown message.