simpleTCP is a minimal non-blocking TCP server written for Python 3.
simpleTCP is written in pure Python 3. It has no external dependencies. To install,
- Clone the repository:
git clone https://github.com/gragas/simpletcp
- Install the package:
python3 setup.py install
simpleTCP just requires that you specify:
- the mode (local or public) of your server
- the port of your server
- what happens when your server receives data
For example, here's an echo server:
from simpletcp.tcpserver import TCPServer
def echo(ip, queue, data):
queue.put(data)
server = TCPServer("localhost", 5000, echo)
server.run()
echo
is the server's callback function. This function is called whenever the server receives data.
Callback functions should take three arguments:
ip
: The IP address that the data was received from.queue
: This is aqueue.Queue
object. Any data put into this queue will be asynchronously sent back to the socket it was received from. In this case, our server echoes all data it receives, so we just put all received data right back into this queue withqueue.put(data)
.data
: This is the data that the server received. It is a string of bytes. Its type isbytes
.
The TCPServer
constructor takes three arguments:
- The
mode
. There are two validmodes
:"localhost"
and"public"
. They are aptly named ---"localhost"
means run the server on127.0.0.1
. Therefore, the server is only visible on the machine on which it is running."public"
means run the server on0.0.0.0
--- make it visible to anything that can see the machine on which it is running. - The port. For development, pick a high (four-digit) number.
- The callback function. This function is called whenever the server receives data.
Examples can be found in the /examples
folder.
Apache Version 2. It looks the coolest.
Thomas Fischer