python的web请求(requests的用法)

import json
import gzip
import urllib
import urllib2
import time
import requests

# ip和模块是容易改动的
URL = "http://127.0.0.1:5000/add_article"
PARAMS = {'uid':1, "head": "head111", "content":"content222"}

def post_form(url, params, method="post"):
    url = URL + url
    params = dict(PARAMS, **params)
    if method == "get":
        data = urllib.urlencode(params)
        url = url+ '?' +data
        print url
        req = urllib2.Request(url)
        r = urllib2.urlopen(req)
    else:
        print url, params
        data = urllib.urlencode(params)
        req = urllib2.Request(url)
        r = urllib2.urlopen(req, data)
        
    print r.text

params = {'account': 3}
post_form('', params, 'post')


你可能感兴趣的:(python的web请求(requests的用法))