cloudbean项目中到php开发web管理端,用python开发服务控制端,在项目中python的服务控制端有时候需要主动连接php的web管理端下载或上传配置参数或数据信息,这里采用的原理是python模拟http客户端,向php所在的apache发起get或post请求。
这里将实现的技术代码进行公开。
一、python以get请求的方式,请求php页面,并获得返回值
1、python代码:
[root@ay python]#cat httpclientget.py #!/usr/bin/env python#coding=utf8import httplibhttpclient=noneprint 'bb'try: httpclient=httplib.httpconnection('www.xxx.org',80) httpclient.request('get','/cloudbean/capture.php?name=liu&pass=wew') res=httpclient.getresponse() print res.status print res.reason print res.read()except exception,e: print heelowr
2、php代码: [root@ay python]#cat /cloudbean/capture.php
二、python以 post请求的方式,请求php页面,并获得返回值
1、python代码:
[root@ay python]#cat httpclientpost.py #!/usr/bin/env python#coding=utf8import httplib,urllibhttpclient=nonetry: 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('www.xxx.org',80) httpclient.request('post','/cloudbean/cappost.php',params,headers) res=httpclient.getresponse() print res.status print res.reason print res.read() print res.getheaders()except exception,e: print error
2、php代码:
[root@ay python]#cat /cloudbean/cappost.php