python发送http post请求

网上看了好多,感觉还是用urllib2最简单

import urllib,urllib2

def doPost(self,data1,data2):
    data = urllib.urlencode({'data1':data1,'data2':data2})
    request = urllib2.Request('http://www.xxx.com/test',data)
    response = urllib2.urlopen(request)
    file = response.read()
    if response.code != 200:
        return 'error code:'+response.code)
    else:
        return file

你可能感兴趣的:(python)