1 | #small client based on the matlab function writeclient |
---|
2 | __author__ = 'delerue' |
---|
3 | |
---|
4 | #import urllib |
---|
5 | import time |
---|
6 | from datetime import datetime |
---|
7 | |
---|
8 | # Echo client program |
---|
9 | def writeclient(HOST,PORT,msg,verbose,waittime=0,logfile='',datedlog=''): |
---|
10 | "Writes msg on host HOST port PORT; verbose if verbose=1" |
---|
11 | import socket |
---|
12 | s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
---|
13 | s.connect((HOST, PORT)) |
---|
14 | if (verbose==1): |
---|
15 | print('Sending message ', msg, ' to host ', HOST, ' on port ', PORT) |
---|
16 | if (datedlog!=''): |
---|
17 | date_str=datetime.now().strftime("_%Y%m%d") |
---|
18 | logfile=datedlog+date_str+".txt" |
---|
19 | if (logfile!=''): |
---|
20 | f = open(logfile, 'a') |
---|
21 | time_str=datetime.now().strftime("%Y%m%d %H:%M:%S") |
---|
22 | f.write("("+time_str+") > "+msg+"\n") |
---|
23 | s.sendall(msg) |
---|
24 | time.sleep(waittime) |
---|
25 | data='' |
---|
26 | dataPart = s.recv(1024) |
---|
27 | data=data+dataPart |
---|
28 | while len(dataPart)==1024: |
---|
29 | dataPart = s.recv(1024) |
---|
30 | data=data+dataPart |
---|
31 | |
---|
32 | s.close() |
---|
33 | if (logfile!=''): |
---|
34 | time_str=datetime.now().strftime("%Y%m%d %H:%M:%S") |
---|
35 | f.write("("+time_str+") < "+data+"\n") |
---|
36 | f.close() |
---|
37 | if (verbose==1): |
---|
38 | print('Received', repr(data)) |
---|
39 | return data |
---|
40 | |
---|
41 | |
---|
42 | #writeclient('10.0.1.161',30000,'ca_va?',1,datedlog='/Users/delerue/Downloads/writeclient_tmp') |
---|
43 | #writeclient('10.0.1.161',30000,'1CE000000000',1,datedlog='/Users/delerue/Downloads/writeclient_tmp') |
---|
44 | #time.sleep(1) |
---|
45 | #writeclient('10.0.1.161',30000,'1CD000000000',1,datedlog='/Users/delerue/Downloads/writeclient_tmp') |
---|
46 | #time.sleep(1) |
---|
47 | #writeclient('10.0.1.161',30000,'1CE000000000',1,datedlog='/Users/delerue/Downloads/writeclient_tmp') |
---|
48 | #time.sleep(1) |
---|
49 | #writeclient('10.0.1.161',30000,'1QP000010000',1,datedlog='/Users/delerue/Downloads/writeclient_tmp') |
---|
50 | #time.sleep(1) |
---|
51 | #writeclient('10.0.1.161',30000,'1GR000000000',1,datedlog='/Users/delerue/Downloads/writeclient_tmp') |
---|