业务需求:模拟TANX给DSP发消息,protobuf数据已弄好。
代码:
def PostDataToDSP(self,url,postdata): headers = {
#taobao文档规定 'Content-Type':'application/octet-stream', #保持长连接 'Connection':'Keep-Alive', } #把protobuf数据转成string payload = postdata.SerializeToString() conn = httplib.HTTPConnection(url) conn.request(method='POST',url='/',body = payload, headers = headers) #获取DSP的应答 response = conn.getresponse() if 200 == response.status: print 'send successfully' else: print 'send failed' conn.close() return response