0

First let me say that I am not a programmer but I have always been able to accomplish what I've needed with Python. My current project is a network encoder for SageTV. SageTV PVR sends commands to a network encoder running on port 4510 which in turn tunes a capture card, then starts ffmpeg recording based on parameters passed. It then continues to listen for further instructions and processes those commands.

I have set up SageTV and can communicate using netcat. This is the output using netcat:

root@debian# nc -l -k -p 4510
VERSION
1
STOP
OK

In the above example. The SageTV server replied 'VERSION' and I typed '1' then it replied 'STOP' and I replied 'OK'.

I have been trying to find a good example of how to convert what netcat is doing into a Python script. I've found various examples but I just can't find one that will listen and even just show what the server is showing. Can someone show me or point me to a good example? I know this should be really easy to do but I've been banging my head on this with no luck. Any help would be greatly appreciated.

1
  • Start with socket(7) to get an idea of what netcat is actually doing. Then take a brief look at socket before continuing on to asyncio and/or socketserver. Commented Sep 29, 2015 at 14:59

1 Answer 1

2

Examle code using python3 and asyncio:

import asyncio

@asyncio.coroutine
def handler(reader, writer):
    def send(msg):
        print("send to device: {}".format(msg))
        writer.write((msg + '\n').encode())

    print("device connected")
    while True:
        msg = yield from reader.readline()
        if not msg:
            print("device disconnected")
            break
        msg = msg.decode().strip()
        print("got from device: {}".format(msg))

        if msg == 'VERSION':
            send('1')
        elif msg == 'STOP':
            send('OK')

loop = asyncio.get_event_loop()
coro = asyncio.start_server(handler, '0.0.0.0', 4510, loop=loop)
server = loop.run_until_complete(coro)
try:
    loop.run_forever()
except KeyboardInterrupt:
    pass
server.close()
loop.run_until_complete(server.wait_closed())
loop.close()

For documentation start from https://docs.python.org/3/library/asyncio.html

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you! This is exactly what I needed. If I may trouble you with only one more thing. The server replies back with a windows path like this: BUFFER SageSlingBox-1 TV Tuner|3|83886080|D:\Media\tvrecordings\SageSlingBox1on1921680204510TVTuner-0.mpgbuf|Best I am trying to pull the filepath out of this jumble but I'm having a world of difficulty as it sees the \tvrecordings as \t vrecordings. Tab vrecordings. Any idea on how to sanitize msg? I tried replacing the \ with / but I can't get that to work.
use escaping by doubling \\: \\tvrecordings

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.