7

I am using an application in C which sends continous data to a specified port. I can view the incoming data using 'Putty' (with telnet 127.0.0.1 port 30003).

Now I want to capture that data for processing. Please advice how to do that in python?

The data flows in line by line, at a rate of 5 lines per second. I should read the data line by line for processing and display it in a window.

Manoj P Joseph
  • 99
  • 1
  • 1
  • 5

2 Answers2

13

This sounds like homework... You haven't tried to do it.

In python, to receive and send data (and definitely exchange data), we use the library called socket. You need two scripts, a server-side (which you've written in C) and a client-side script.

# client example

import socket, time
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client_socket.connect(('localhost', 5000))
while True:
    time.sleep(5)
    data = client_socket.recv(512)
    if data.lower() == 'q':
        client_socket.close()
        break

    print("RECEIVED: %s" % data)
    data = input("SEND( TYPE q or Q to Quit):")
    client_socket.send(data)
    if data.lower() == 'q':
        client_socket.close()
        break

This is a client-side script example, which receives the data each 5 secs and prints it out. I hope you can adapt it to fit your needs.

Source: Basic Python client socket example

knia
  • 463
  • 6
  • 16
  • Hai, sorry for not giving a try. Actually I tried several opions but failed. Your codes works perfectly. Can I ask a qustion? As I mentioned the data flow is not continous, it is silent for some time, then comes like that. So is there an option other than a loop so that data is recieved when it comes rather than checking it in every 5 seconds?? Thanks – Manoj P Joseph Dec 27 '14 at 23:29
  • @ManojPJoseph maybe avoiding that loop but always using a loop. A server is always checking if there are any incoming connections. A infinite loop works just fine, a lot of things work that way. I also sometimes try to avoid them but you will always have to do some of the kind (I speak from my experience, there may be something I do not know). –  Dec 27 '14 at 23:35
2

Install tcpflow

Run the given script

import os

INTERFACE = "lo"

PORT = "30003"

os.system("tcpflow -i %s port %s" % (INTERFACE, PORT))

It will write the requests which are coming to the port into a file like 127.000.000.001.06080-127.000.000.001.6347 in script's location

hariK
  • 2,722
  • 13
  • 18
  • 1
    He says `capture the data for processing`` –  Dec 26 '14 at 17:03
  • I finally wrote the program to recieve data through a socket: from socket import * HOST = 'localhost' PORT = 30003 #our port from before ADDR = (HOST,PORT) BUFSIZE = 4096 sock = socket( AF_INET,SOCK_STREAM) sock.connect((ADDR)) def readlines(sock, recv_buffer=4096, delim='\n'): buffer = '' data = True while data: data = sock.recv(recv_buffer) buffer += data while buffer.find(delim) != -1: line, buffer = buffer.split('\n', 1) yield line return for line in readlines(sock): print line – Manoj P Joseph Jan 22 '15 at 03:23