simpleTCP is a minimal non-blocking TCP server written for Python 3. It is licensed under the GNU Affero General Public License, Version 3.
simpleTCP is no longer available on PyPI. To install, run python3 setup.py install
.
simpleTCP is written in pure Python 3. It has no external dependencies.
simpleTCP just requires that you specify:
- the mode (local or public) of your server
- your server can also be bound to a specific IP address
- 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 specialmodes
:"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. If mode is not"localhost"
or"public"
, then it is interpreted as an IP address. - The port. For development, pick a high (four-digit) number.
- The callback function. This function is called whenever the server receives data.
To send data to the server, create a ClientSocket
and send away!
from simpletcp.clientsocket import ClientSocket
s1 = ClientSocket("localhost", 5000, single_use=False)
response = s1.send("Hello, World!")
Examples can be found in the /examples
folder.