python发送HTTP GET请求

来源:互联网 发布:nginx搭建web服务器 编辑:程序博客网 时间:2024/05/20 06:10

         最近做的一个模块里需要发送GET请求,带参数的,查了网上很多例子,以及官方文档,都没交代清楚要如何做,大部分都是直接请求了一个页面。官方例子里,只有post请求是比较详细的。没办法,只好自己动手做实验了。现在把实验结果总结如下:

         1.通过 httplib模块

conn = httplib.HTTPConnection(Addr,timeout=7) url=/game/user/info?id=28 conn.request("GET",url) conn.request("GET",url,'',headers) response = conn.getresponse() Data=response.read()  

             这里有必要提一下request的定义:

            

   def request(self, method, url, body=None, headers={}):
        """Send a complete request to the server."""

        try:
            self._send_request(method, url, body, headers)
        except socket.error, v:
            # trap 'Broken pipe' if we're allowed to automatically reconnect
            if v.args[0] != 32 or not self.auto_open:
                raise
            # try one more time
            self._send_request(method, url, body, headers)

 

          对于GET请求来说,因为参数已经带在url里,body是空的,而对于POST来说,body就是发送出去的参数了。

 

           2.通过urllib2

 

           url=/game/user/info?id=28

           full_url='http://192.168.1.250'+url.

           data=urllib2.urlopen(full_url)
           Data=data.read()

原创粉丝点击