在python中发送一个http get请求

时间:2013-07-26 10:28:10

标签: python

我正在尝试向"http://192.168.1.236:8081/send.php"发送HTTP get请求,代码为:

params = urllib.urlencode({'content': str(error)})        
head = {'Host': '192.168.1.236', 'User-Agent':  'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:22.0) Gecko/20100101 Firefox/22.0',
'Accept': "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
"Accept-Language": "zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3",
"Accept-Encoding": "gzip, deflate", "Connection":   "keep-alive"}
conn = httplib.HTTPConnection(IP, PORT)
conn.request(method="GET", url="/send.php", body=params, headers=head)
r = conn.getresponse()
print r.read()
conn.close()

然后打印结果是'Array \ n(\ n)\ nmail content empty'。但是通过模拟在brower中发送带有url "http://192.168.1.236:8081/send.php?content=11212121212121212"的get请求的结果是“Array([内容] ] => 11212121212121212)Array()邮件内容为空“。谁能告诉我可能出现的问题。

1 个答案:

答案 0 :(得分:2)

您可能需要将params放在URL上,GET请求通常不会有我认识的主体。

conn.request(method="GET", url="/send.php?%s" % (params,), headers=head)