The idea I have is to make a netcat server, and when you connect to it, the server runs a python script with which the user can interact. So for example the script asks for a number, and it outputs that number to the power of 3. How can you do that? How do you run a program and redirect the output to the connected user and make them able to interact with it in real time?

Pseudo output:

$ nc  
Input a number: 2  

The script:

print(str(int(input("Input a number: "))**3))
  • 1
    If you involve Python might as well do the networking in Python too...? – frostschutz Oct 19 at 23:24
  • @frostschutz can you give an example? not sure where to start, im a bit of a noob – InxaneNinja Oct 19 at 23:41
  • See docs.python.org/3/library/socket.html for an example of IPv4 echo server. Hint, you don't need client - you can just use nc. If you still cannot get the code working, post whatever code you've written so far ( even if it doesn't work ) and then we can discuss how to improve that. – Sergiy Kolodyazhnyy Oct 20 at 9:42
socat TCP-LISTEN:50011,fork EXEC:'/path/to/script',stderr,pty,echo=0


  • 50011 is the listening port, you can choose your own. Connect to it later, e.g. nc 50011.
  • fork allows socat to serve many clients (also simultaneously).
  • Without pty it's normal to get Input a number: only after you actually provided input blindly.
  • My tests indicate that if one terminates socat with Ctrl+C then the port may stay in use for a while. If it's a problem, consider reuseaddr (i.e. TCP-LISTEN:50011,fork,reuseaddr).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.