HTTP请求的问题

时间:2011-08-15 07:05:17

标签: http get

我正在尝试使用python发出http请求:

class DownloadManager():
    def __init__(self, servername):
        self.conn = httplib.HTTPConnection(servername)
        print self.conn

    def download(self, modname):
        params = urllib.urlencode({"name" : modname})
        self.conn.request("GET", "/getmod", params)
        resp = self.conn.getresponse()
        print resp.status
        print resp.reason
        if resp.status == 200:
            url = resp.read()
        else:
            return

        mod = urllib2.urlopen(url)
        return mod.read()

但是得到: 400 Bad request

在服务器日志中,我看到:

WARNING  2011-08-15 06:58:39,692 dev_appserver.py:4013] Request body in GET is not permitted: name=Test
INFO     2011-08-15 06:58:39,692 dev_appserver.py:4248] "GET /getmod HTTP/1.1" 400 -

怎么了?

1 个答案:

答案 0 :(得分:2)

GET请求方法在正文中没有任何内容。如果要通过GET方法传递参数,则必须在问号“?”后将URL编码参数添加到URL中。字符:

params = urllib.urlencode({"name" : modname})
self.conn.request("GET", "/getmod?%s" % params)

但是,您真正想要做的是POST请求:

params = urllib.urlencode({"name" : modname})
self.conn.request("POST", "/getmod", params)