在python中,模拟http客户端发送get和post请求,主要用httplib模块的功能。

1、python发送GET请求

我在本地建立一个测试环境,test.php的内容就是输出一句话:

1
echo 'Old friends and old wines are best.';

python发送get请求代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#!/usr/bin/env python
#coding=utf8
 
import httplib
 
httpClient = None
 
try:
    httpClient = httplib.HTTPConnection('localhost'80, timeout=30)
    httpClient.request('GET''/test.php')
 
    #response是HTTPResponse对象
    response = httpClient.getresponse()
    print response.status
    print response.reason
    print response.read()
except Exception, e:
    print e
finally:
    if httpClient:
        httpClient.close()

上面代码中使用了finally来保证即使出错的时候也能关闭httpClient。运行这个程序,在我的电脑上输出结果如下:

python用httplib发送get请求

2、python发送POST请求

修改test.php内容,打印出$_POST数组:

1
var_dump($_POST);

python发起post请求代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#!/usr/bin/env python
#coding=utf8
 
import httplib, urllib
 
httpClient = None
try:
    params = urllib.urlencode({'name''tom''age'22})
    headers = {"Content-type""application/x-www-form-urlencoded"
                    "Accept""text/plain"}
 
    httpClient = httplib.HTTPConnection("localhost"80, timeout=30)
    httpClient.request("POST""/test.php", params, headers)
 
    response = httpClient.getresponse()
    print response.status
    print response.reason
    print response.read()
    print response.getheaders() #获取头信息
except Exception, e:
    print e
finally:
    if httpClient:
        httpClient.close()

运行代码,在我的电脑上输出如下:

python用httplib发送post请求

python用httplib模块发送get和post请求的更多相关文章

  1. 利用python httplib模块 发送Post请求测试web服务是否正常起来!

    最近在学习python,恰好老大最近让我搞个基于post请求测试web服务是否正常启用的小监控,上网查了下资料,发现强大的Python恰好能够用上,所以自己现学现卖,顺便锻炼下自己. 由于本人也刚接触 ...

  2. Python 使用 requests 模块发送请求的使用及封装

    一.requests 模块基本使用 1.准备接口的URL.请求参数.请求头 # 1. 构造注册.登录.充值请求的url register_url = "注册url" login_u ...

  3. python之使用request模块发送post和get请求

    import requestsimport json #发送get请求并得到结果# url = 'http://api.nnzhp.cn/api/user/stu_info?stu_name=小黑马 ...

  4. Python接口测试-使用requests模块发送GET请求

    本篇主要记录下使用python的requests模块发送GET请求的实现代码. 向服务器发送get请求:无参数时:r = requests.get(url)带params时:r = requests. ...

  5. Python接口测试-使用requests模块发送post请求

    本篇主要记录下使用python的requests模块发送post请求的实现代码. #coding=utf-8 import unittest import requests class PostTes ...

  6. python urllib2 httplib HTTPConnection

    httplib实现了HTTP和HTTPS的客户端协议,一般不直接使用,在python更高层的封装模块中(urllib,urllib2)使用了它的http实现. import httplib conn  ...

  7. 洗礼灵魂,修炼python(59)--爬虫篇—httplib模块

    httplib 1.简介 同样的,httplib默认存在于python2,python3不存在: httplib是python中http协议的客户端实现,可以用来与 HTTP 服务器进行交互,支持HT ...

  8. Python学习_Python 3.X版本导入httplib模块报ImportError解决方案

    之前用Python 2.7版本的httplib做接口测试时,运行代码都是正常的, 最近开始用Python 3.3之后,再去看以前的代码,发现import httplib出现错误:Unresolved ...

  9. Python第十三天 django 1.6 导入模板 定义数据模型 访问数据库 GET和POST方法 SimpleCMDB项目 urllib模块 urllib2模块 httplib模块 django和web服务器整合 wsgi模块 gunicorn模块

    Python第十三天   django 1.6   导入模板   定义数据模型   访问数据库   GET和POST方法    SimpleCMDB项目   urllib模块   urllib2模块 ...

随机推荐

  1. asp.net web forms和asp.net mvc比较

    ASP.NET Webforms Behind Code的好处和存在的问题 ASP.NET Webforms是一个RAD/VISUAL(快速可视化)的Web程序开发技术.也就是说,开发者简单地拖拽控件 ...

  2. 【转】DB2 常用命令

    1. 打开命令行窗口  #db2cmd 2. 打开控制中心  # db2cmd db2cc 3. 打开命令编辑器  db2cmd db2ce =====操作数据库命令===== 4. 启动数据库实例  ...

  3. 【JavaScript学习笔记】hello world

    <html> <body> <script language="JavaScript"> alert("Hello world&quo ...

  4. Myeclipse提示失效?

  5. ssh-keygen的用法

    一.概述 1.就是为了让两个linux机器之间使用ssh不需要用户名和密码.采用了数字签名RSA或者DSA来完成这个操作 2.模型分析 假设 A (192.168.20.59)为客户机器,B(192. ...

  6. 【转】15 个用于 GitHub 的 Chrome 插件

    原文网址:http://webres.wang/15-useful-chrome-extensions-for-github/ 对于 GitHub 你了解多少呢?其实,它是一个基于 Git 托管的 W ...

  7. Android RecyclerView使用详解(三)

    在上一篇(RecyclerView使用详解(二))文章中介绍了RecyclerView的多Item布局实现,接下来要来讲讲RecyclerView的Cursor实现,相较于之前的实现,Cursor有更 ...

  8. js内置对象-Date对象

    Date对象: Data对象可以储存任意一个日期,并且可以精确到毫秒数(1/1000 秒). 定义: //默认初始值定义: var dataName=new Date(); /*使用关键字new;Da ...

  9. CRF模型

    CRF的全称是Conditional Random Fields,由CMU教授John Lafferty 提出,原文标题:Conditional R andom Fields: Probabilist ...

  10. 【LeetCode】223 - Rectangle Area

    Find the total area covered by two rectilinear rectangles in a 2D plane. Each rectangle is defined b ...