码迷,mamicode.com
首页 > Web开发 > 详细

HTTPResponse.read([amt]):只能read一次

时间:2014-11-04 21:06:40      阅读:204      评论:0      收藏:0      [点我收藏+]

标签: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一次

标签:style   blog   http   io   color   os   sp   div   on   

原文地址:http://www.cnblogs.com/HpuAcmer/p/4074703.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!