Python embedded in bash not working -
i want embed small python script inside bash script can send json object socket. have tried following code:
python -c "$(cat << 'eof' import socket import json data = {'ip':192.168.1.150', 'status':'up'} s = socket.socket(socket.af_inet, socket.sock_stream) s.connect(('127.0.0.1', 13373)) s.send(json.dumps(data)) result = json.loads(s.recv(1024)) print result s.close() eof )"
and this: python -c " import socket import json
data = {'ip':192.168.1.150', 'status':'up'} s = socket.socket(socket.af_inet, socket.sock_stream) s.connect(('127.0.0.1', 13373)) s.send(json.dumps(data)) result = json.loads(s.recv(1024)) print result s.close() "
but keep getting following error:
data = {'ip':192.168.1.150', 'status':'up'} ^ syntaxerror: invalid syntax
i'm assuming because bash interpreting this, not python. i've tested code in python script, , works. also, need -c option?
apologies, i'm inexperienced in python, i've written quite extensive bash scripting project i'm working on , need send output of these sockets json objects. such small snipped of embedded python code seems far simplest answer, unless has other suggestions.
python version installed on centos python 2.6.6
the problem you're having results in syntaxerror
don't have opening single quote on ip value in data
dict. have:
data = {'ip':192.168.1.150', 'status':'up'}
you need:
data = {'ip':'192.168.1.150', 'status':'up'}
Comments
Post a Comment