如何在python中使用action = fetch向URL发送HTTP POST请求?

时间:2013-12-13 16:01:24

标签: python http httprequest

我想向网址发送HTTP POST请求。 响应将是一个有效的JSON对象。

我为此制作了这个小python脚本,但遗憾的是它无法正常工作。

import httplib, urllib

host = 'www.google.com'
url = 'www.google.com'


values = urllib.urlencode(values)

conn = httplib.HTTPSConnection(host)
conn.request("POST", url)
response = conn.getresponse()

data = response.read()

print 'Response: ', response.status, response.reason
print data
print response

1 个答案:

答案 0 :(得分:1)

使用requests

  import requests, time
  url = 'www.something.com/nextstep'
  headers = {'Content-Type': 'application/x-www-form-urlencoded'}
  values = {'action' : 'fetch', 'number' : '1'}
  r = requests.post(url, data=values, headers=headers)
  uuid = r.json()['uuid']       

  time.sleep(1)
  values = {'action':'solve', 'number':1, 'uuid':''}
  url = 'www.something.com/anothernextstep'
  r = requests.post(url, data=values, headers=headers)