Simple TCP and UDP network server in Python

Something ‘netcat’

One of the things that I find lacking in netcat is a timestamp feature. You can log the requests but you can not easy log the exact timestamps. Instead of writing a wrapper around netcat I wrote a small python script that can act as a simple TCP or UDP network server.

A lot of the code is inspired on code at http://ilab.cs.byu.edu/python/select/echoserver.html and http://www.binarytides.com/udp-socket-programming-in-winsock/

Github

All of the code can be found on Github together with a basic UDP client and server script. You can download the scripts easily

svn export https://github.com/cudeso/tools/trunk/network-servers

Feel free to contribute.

simple-server.py

The script requires a couple of parameters

  1. -p –port : the network port;
  2. -t –protocol : the network protocol;
  3. -l –logfile : where to log the requests;
  4. -i –ip : the ip of the server, only used for logging (can be free text);
  5. -e –echo (not mandatory) : reply the request (default ‘echo’);
  6. -s –single (not mandatory) : stop after one single request;

The request size in the script is limited to 1024. No other sanity checks are done.

Start it with

./simple-server.py -p 9898 -t udp -l 9898.udp.log -i 192.168.1.1

Because of the lack of security checks it’s best to run this simple server in a disposable virtual server.

2 thoughts on “Simple TCP and UDP network server in Python

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.