返回顶部

收藏

python使用urllib2模拟http post

更多

使用urllib2.urlopen方法模拟http post请求:

def post_data(app_para_dct):
  content = ""
  if app_para_dct:
    para_dct = {}
    para_dct['a'] = 'a'
    para_dct['b'] = 'b'
    url = """http://gw.api.taobao.com/router/rest"""
    para_data = urllib.urlencode(para_dct)
    f = urllib2.urlopen(url, para_data)
    content = f.read()
  return content

使用request.add_data添加post数据

def call_api(url, **kwargs):
    req = urllib2.Request(url)

    if kwargs:
        req.add_data(urllib.urlencode(kwargs))

    auth = 'Basic ' + base64.urlsafe_b64encode("%s:%s" % (USER, PASS))
    req.add_header('Authorization', auth)

    return urllib2.urlopen(req).read()

标签:python,urllib2,http

收藏

0人收藏

支持

1

反对

0

发表评论