GET:
capture.php:
echo $_GET["name"];
echo $_GET["pass"];
echo "GET:";
?>
python27get01.py (python2.7.12):
#!/usr/bin/env python
#coding=utf8
import httplib
httpclient=None
print 'bb'
try:
httpclient=httplib.HTTPConnection('api2016.hoopuu.com',80)
httpclient.request('GET','/capture.php?name=liu&pass=wew')
res=httpclient.getresponse()
print res.status
print res.reason
print res.read()
except Exception,e:
print "heelowr"
POST:
cappost.php:
echo $_POST["name"];
echo $_POST["pass"];
echo "post";
?>
python27post1.py (暂不成功,python2.7.12) #!/usr/bin/env python #coding=utf8 import httplib,urllib httpclient=None try: params = urllib.urlencode({'name': 'tom', 'pass': 22}) print 'aaa' headers = {"X-CPU": "arm/x86" , "X-USER": "234299044218541","X-RESOLUTION": "240x320","X-SYSTEM":"2.3","X-LANG":"en-us","X-SIGN":"a1ae6bee406a6b8aa0862969ba49cc1d", "X-IMSI":"3110012345678912","X-COUNTRY":"in","X-TIME":"1335339139"} httpclient=httplib.HTTPConnection('api2016.hoopuu.com',80) httpclient.request('POST','/cappost.php',params,headers) res=httpclient.getresponse() print res.status print res.reason print res.read() print res.getheaders() except Exception,e: print "error" 再Post: <?php echo $_POST["j_username"]; echo $_POST["j_password"]; echo "post"; ?> #在python2.7.12运行! import urllib,urllib2 org_name = "henry_test" org_psw = "XXXXX" login_url = "http://api2016.hoopuu.com/cappost.php"#请求的url para = { 'j_username':org_name, 'j_password':org_psw, }#参数为用户名和密码 post_data = urllib.urlencode(para)#进行url编码 req = urllib2.Request(login_url,post_data)#生成Request对象 resp = urllib2.urlopen(req)#open it file=resp.read()#读出返回结果 print file #打印出返回结果 python3: <?php echo $_POST["j_username"]; echo $_POST["j_password"]; echo "post"; ?> python3(python3版本): from urllib import request, parse # Base URL being accessed url = 'http://api2016.hoopuu.com/cappost.php' #httpbin.org/post' # Dictionary of query parameters (if any) parms = { 'j_username':'terry',#'name1' : 'value1', 'j_password':'aw3', #'name2' : 'value2' } # Encode the query string querystring = parse.urlencode(parms) # Make a POST request and read the response u = request.urlopen(url, querystring.encode('ascii')) resp = u.read() print(resp) 参考文献: http://python3-cookbook.readthedocs.io/zh_CN/latest/c11/p01_interact_with_http_services_as_client.html