1

I am getting the data from websocket (ws://localhost:8080) and doing command line args with that. code mentioned below..

python datapy.py -i 127.0.0.1 -p 8080

from websocket import create_connection
#import ConfigParser
#from test import settings
import sys
import argparse
import socket 

parser = argparse.ArgumentParser()
parser.add_argument('-i', dest='ip' , help='host name / IP')
parser.add_argument('-p', dest='port' , type=int, help='port')
parser.add_argument('-t', dest='time' , type=int, help='Time (in seconds) to keep reading data from websocket. After this process will be exit. Default value is 0. For 0 it will run infinitely')
parser.add_argument('-m', dest='msg' , type=int, help='Number of message. Process will exit after receiving Number of messages. Default is 0, means runs for ever.')
args = parser.parse_args()
print(args.ip)

#ws://%s:%s" % (args.ip,args.port)
url = "ws://%s:%s" % (args.ip,args.port)
ws = create_connection(url)
print "Receiving data from the socket..."
for each in ws:
    result =  ws.recv();
    print "Received '%s'" % result
ws.close();

Console:

C:\Users\556718\Desktop\pythonprac>python datapy.py -i 127.0.0.1 -p 8080
127.0.0.1
Receiving data from the socket...
Received 'sound'
Received 'eokeoe'
Received 'mdmmd'
Received 'ssss'
Received 'tttt'

As mentioned in argparse, I want to pass -m and -t - like

python data.py -i 127.0.0.1 -p 8080 -m 5 --> It will limit to only 5 messages in the console.. 
python data.py -i 127.0.0.1 -p 8080 -t 120 --> Send me the messages until 120 seconds - after that exit .... 
python data.py -i 127.0.0.1 -p 8080 -m 5 -t 120 --> for limiting to 120 seconds (2 minutes) and 5 messages - either of the condition  

Can anyone help me in this

Soundarya Thiagarajan
  • 574
  • 2
  • 13
  • 31

1 Answers1

1

I couldn't test, but something along these lines should work.

from websocket import create_connection
#import ConfigParser
#from test import settings
import sys
import argparse
import socket 
import time

parser = argparse.ArgumentParser()
parser.add_argument('-i', dest='ip' , help='host name / IP')
parser.add_argument('-p', dest='port' , type=int, help='port')
parser.add_argument('-t', dest='time' , type=int, help='Time (in seconds) to keep reading data from websocket. After this process will be exit. Default value is 0. For 0 it will run infinitely')
parser.add_argument('-m', dest='msg' , type=int, help='Number of message. Process will exit after receiving Number of messages. Default is 0, means runs for ever.')
args = parser.parse_args()
print(args.ip)

#ws://%s:%s" % (args.ip,args.port)
url = "ws://%s:%s" % (args.ip,args.port)
ws = create_connection(url)
print "Receiving data from the socket..."
tstart = time.time()
nbmsg = 0
for each in ws:
    result =  ws.recv();
    nbmsg += 1
    if args.time is not None:
        if time.time() > tstart + args.time:
            print('Time limit reached.')
            sys.exit()
    if args.msg is not None and nbmsg >= args.msg:
        print('Message limit reached')
        sys.exit()
    print "Received '%s'" % result
ws.close();
Loïc
  • 11,804
  • 1
  • 31
  • 49
  • Loic - Message part works perfect. Time part doesnt work fine now.. C:\Users\556718\Desktop\pythonprac>python soufunc.py -i 127.0.0.1 -p 8080 -t 1 if I give like this ., it is not exiting after 1 second. – Soundarya Thiagarajan Aug 25 '16 at 09:31
  • truth is you code isn't asynchronous, so the test on time is done after a message is received, so if you don't receive message for 10 seconds, and you have t=1 it will still take 10 seconds to shut the program. What you could do though is use asyncio (https://github.com/aaugustin/websockets/blob/master/example/client.py) or threads, in order to fix that problem. – Loïc Aug 26 '16 at 08:15
  • Oh okay. Will have a look at it. and also I have one more question - If the user wants to interrupt by pressing Ctrl - C when the code is running it is not actually exiting. Is there any way to fix that issue? – Soundarya Thiagarajan Aug 26 '16 at 08:41
  • probably because `ws.recv()` uses `try / except` without specifying the exception type (thus catching keyboardInterrupt). This issue would also be fixed using an asynchronous websocket client. Maybe you can also fix it using `signal` library : http://stackoverflow.com/questions/1112343/how-do-i-capture-sigint-in-python – Loïc Aug 26 '16 at 08:57
  • I tried using signal library .. I am using Windows .. Signal supports in Unix / linux .. No support for signal.pause() on Windows.. – Soundarya Thiagarajan Aug 26 '16 at 09:05