Python向PHP发起GET与POST请求
一、Python以GET请求的方式,请求PHP页面,并获得返回值
print 'aaa'#!/usr/bin/env python
《Python核心编程 第二版》.(Wesley J. Chun ).[高清PDF中文版]
echo $_GET["name"];print "heelowr"
res=httpclient.getresponse()
echo $_GET["pass"];
<?php
echo $_POST["name"];
2、PHP代码:
except Exception,e:Python 的下载地址:请点这里?>
echo $_POST["pass"];
import httplib,urllib
httpclient=httplib.HTTPConnection('www.xxx.org',80)print 'bb'
这里将实现的技术代码进行公开。
在Ubuntu下用Python搭建桌面算法交易研究环境
print res.statusheaders = {"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"}
#coding=utf8 print res.getheaders()
<span xmlns="http://www.w3.org/1999/xhtml">[root@AY python]#cat /CloudBean/capture.php
except Exception,e:
print res.reason
httpclient=None
2、php代码:
</span>二、Python以Post请求的方式,请求PHP页面,并获得返回值
print res.read()print res.status
1、Python代码:
CloudBean项目中到PHP开发WEB管理端,用Python开发服务控制端,在项目中Python的服务控制端有时候需要主动连接PHP的WEB管理端下载或上传配置参数或数据信息,这里采用的原理是Python模拟Http客户端,向PHP所在的Apache发起Get或Post请求。
echo "GET:";<?php
httpclient=httplib.HTTPConnection('www.xxx.org',80)
params = urllib.urlencode({'name': 'tom', 'pass': 22})
httpclient=None
res=httpclient.getresponse()
echo "post";
<span xmlns="http://www.w3.org/1999/xhtml">[root@AY python]#cat /CloudBean/cappost.php
</span>
<span xmlns="http://www.w3.org/1999/xhtml">[root@AY python]#cat httpclientpost.py
Python脚本获取Linux系统信息
</span>#coding=utf81、Python代码:
try:Python 语言的发展简史
#!/usr/bin/env pythonimport httplib
Python 的详细介绍:请点这里
print "error"
try:
httpclient.request('GET','/CloudBean/capture.php?name=liu&pass=wew')
httpclient.request('POST','/CloudBean/cappost.php',params,headers)
?>
</span>
<span xmlns="http://www.w3.org/1999/xhtml">[root@AY python]#cat httpclientget.py
print res.read()
《Python开发技术详解》.( 周伟,宗杰).[高清PDF扫描版+随书视频+代码]