首页 > 代码库 > HTTPResponse.read([amt]):只能read一次
HTTPResponse.read([amt]):只能read一次
业务需要:我要写个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一次
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。