Python向PHP发起GET与POST请求


     CloudB项目中到PHP开发WEB管理端,用Python开发服务控制端,在项目中Python的服务控制端有时候须要主动连接PHP的WEB管理端下载或上传配置參数或数据信息,这里採用的原理是Python模拟Httpclient,向PHP所在的Apache发起Get或Post请求。

     这里将实现的技术代码进行公开。


 一、Python以GET请求的方式,请求PHP页面,并获得返回值

1、python代码:      

<span xmlns="http://www.w3.org/1999/xhtml" style="">[root@AY python]#cat httpclientget.py 
#!/usr/bin/env python
#coding=utf8

import httplib

httpclient=None
print '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"
</span>

2、php代码:     
<span xmlns="http://www.w3.org/1999/xhtml" style="">[root@AY python]#cat /CloudBean/capture.php 
<?php 
  echo $_GET["name"];
  echo $_GET["pass"];
  echo "GET:";
?

> </span>


二、Python以 Post请求的方式,请求PHP页面。并获得返回值

1、Python代码:

<span xmlns="http://www.w3.org/1999/xhtml" style="">[root@AY python]#cat httpclientpost.py 
#!/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('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"
</span>
2、PHP代码:

<span xmlns="http://www.w3.org/1999/xhtml" style="">[root@AY python]#cat /CloudBean/cappost.php 
<?php 
  echo $_POST["name"];
  echo $_POST["pass"];
  echo "post";
?

> </span>




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Python中,你可以使用`requests`库来发起HTTP请求。如果你需要并发发起不同的POST请求,可以使用`concurrent.futures`模块中的`ThreadPoolExecutor`或`ProcessPoolExecutor`类来实现。 以下是一个使用`ThreadPoolExecutor`类并发发起不同的POST请求的示例代码: ```python import requests from concurrent.futures import ThreadPoolExecutor def make_post_request(url, data): response = requests.post(url, data=data) return response.text url_list = ['http://example.com/post1', 'http://example.com/post2', 'http://example.com/post3'] data_list = [{'key1': 'value1'}, {'key2': 'value2'}, {'key3': 'value3'}] with ThreadPoolExecutor(max_workers=3) as executor: future_to_url = {executor.submit(make_post_request, url, data): url for url, data in zip(url_list, data_list)} for future in concurrent.futures.as_completed(future_to_url): url = future_to_url[future] try: response = future.result() except Exception as exc: print('%r generated an exception: %s' % (url, exc)) else: print('%r page is %d bytes' % (url, len(response))) ``` 在这个示例中,我们首先定义了一个`make_post_request`函数,它接收一个URL和一个数据字典,并使用`requests.post`方法发起一个POST请求。然后我们定义了一个URL列表和一个数据字典列表,每个URL和数据字典对应一个POST请求。接下来,我们使用`ThreadPoolExecutor`类创建了一个线程池,将每个POST请求作为一个任务提交到线程池中。线程池的`max_workers`参数指定了最大并发线程数。最后,我们使用`concurrent.futures.as_completed`函数迭代所有任务的结果,对于每个任务,我们可以获取其对应的URL和响应结果,然后进行处理。 你也可以使用`ProcessPoolExecutor`类来发起并发的POST请求,只需要将`ThreadPoolExecutor`替换为`ProcessPoolExecutor`即可。但需要注意的是,由于`ProcessPoolExecutor`使用了进程池而不是线程池,因此传递给`make_post_request`函数的数据必须是可序列化的。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值