python处理http请求

__author__ = 'yangjing'
#-*-coding:utf8-*-
import httplib,json,urllib,urllib2
hostname = "***.***.com"


def getRquest(requestUrl,requestHeader):
    getUrl = "http://"+hostname+requestUrl
    conn = httplib.HTTPConnection(hostname)
    conn.request(method="GET",url=getUrl,body="",headers=requestHeader)
    response = conn.getresponse()
    res= response.read()
    result = json.loads(res)
    print result
    return result


def postRequest(requestUrl,requestHeader,postData):
    postUrl = "http://"+hostname+requestUrl
    conn = httplib.HTTPConnection(hostname)
    conn.request(method="POST",url=postUrl,headers=requestHeader,body=postData)
    response = conn.getresponse()
    res= response.read()
    result = json.loads(res)
    print result
    return result


# 调用postRequest函数时,需要对post的数据进行处理
# 如果是json格式的数据:json.JSONEncoder().encode(data)
# 如果是普通list数据:urllib.urlencode(data)
# requestHeader格式为:{"a":"1","b":"2"}

你可能感兴趣的:(软件测试,python,python,json,http)