Python 两种POST请求的方式

#方式1: urllib2 urllib 以表单的形式提交POST数据
#Exp: username=zcs&password=123

import urllib2
import urllib

data = {'username': 'rose', 'password': '123456'}
f = urllib2.urlopen(
        url='http://IP:PORT/test',
        data=urllib.urlencode(data)
  )
print f.read()

#方式2:requests 以JSON的形式提交数据
#Exp:{"username":"jack","password":"123"}

import requests

data = '{"username":"jack","password":"123"}'
headers = {'Content-Type':'application/json'}
rep = requests.post(url=config['request']['DeleteGraph'], data=data, headers=headers)
return rep.text

你可能感兴趣的:(Python)