这是我在Node-Red中的流程
我只是将一个字符串注入到我的服务器的tcp节点,然后我的服务器将该值作为字符串返回。但是,我继续从Node-Red获得JSON响应,如下面的{"hello":""} where the value is empty and key is hello
。
代码:
while True:
#Receiving from client
data = conn.recv(4096)
all_data += data
print 'Received Message : ' + all_data
if not data:
print 'Final Received Message : ' + all_data
headers = {'Content-Type','text/plain'}
req = urllib2.Request('<my server link>', headers)
try:
urllib2.urlopen(req, str(all_data))
except urllib2.HTTPError, error:
print "error posting the request " + error.reason
break
更新: 即使在我的代码中我返回一个字符串,我仍然在Node-Red中接收一个JSON响应(检查上面的调试结果),任何想法为什么?
答案 0 :(得分:0)
问题在于python,它应该是这样的:
while True:
#Receiving from client
data = conn.recv(4096)
all_data += data
print 'Received Message : ' + all_data
if not data:
print 'Final Received Message : ' + all_data
headers = {'Content-Type':'text/plain'}
req = urllib2.Request('<my server link>', None, headers)
try:
urllib2.urlopen(req, str(all_data))
except urllib2.HTTPError, error:
print "error posting the request " + error.reason
break
None
。