標籤:style blog http io color os sp div on
業務需要:我要寫個tanx模擬器,給DSP發競價請求。
下面是部分代碼:
def PostDataToDSP(self,url,postdata): headers = { ‘Content-Type‘:‘application/octet-stream‘, ‘Connection‘:‘Keep-Alive‘, } payload = postdata.SerializeToString() conn = httplib.HTTPConnection(url) conn.request(method=‘POST‘,url=‘/bid‘,body = payload, headers = headers) response = conn.getresponse() print response.status if 200 == response.status: print response.read() print ‘send successfully‘ else: print ‘send failed‘ conn.close() return response.read()
這個代碼是錯誤的,剛接觸互連網行業,一堆不懂。
老大指點說:只能read一次。
代碼修改後:
def PostDataToDSP(self,url,postdata): headers = { ‘Content-Type‘:‘application/octet-stream‘, ‘Connection‘:‘Keep-Alive‘, } payload = postdata.SerializeToString() conn = httplib.HTTPConnection(url) conn.request(method=‘POST‘,url=‘/bid‘,body = payload, headers = headers) response = conn.getresponse() print response.status if 200 == response.status: res = response.read() print ‘send successfully‘ else: print ‘send failed‘ conn.close() return res
HTTPResponse.read([amt]):只能read一次