python内建模块发起HTTP(S)请求
一、Python2
httplib
简介:httplib实现了HTTP和HTTPS的客户端协议,一般不直接使用,在python更高层的封装模块中(urllib,urllib2)使用了它的http实现。
httplib实现http请求
import httplib
host = ‘www.baidu.com’ # 注意:不能带上协议
port = 80
# 获取HTTPConnection对象
conn = httplib.HTTPConnection(host, port)
# 发起请求
conn.request("GET", "/")
# 获取返回值
res = conn.getresponse()
print res.status # 状态码
print res.read() # 返回结果
urllib、urllib2
简介:urllib 和urllib2都是接受URL请求的相关模块,但是urllib2可以接受一个Request类的实例来设置URL请求的headers,urllib仅可以接受URL。
urllib发起请求
import urllib
baidu = urllib.urlopen('http://www.baidu.com')
print baidu.read() # 读取返回结果
print baidu.geturl() # 获取请求url
print baidu.getcode() # 获取状态码
baidu.close()
urllib进行url编码
import urllib
d = {
"$count":"count",
"$limit":"limit",
"$offset":"offset",
"$filter":None,
}
print urllib.urlencode(d) 结果:%24offset=offset&%24limit=limit&%24filter=None&%24count=count
详细使用方法见
urllib2
import json
import urllib
import urllib2
#get response
data = json.dumps(body)
conn = urllib2.Request(url, data, header)
res = urllib2.urlopen(conn)
print res
print res.read()
二、python3
urllib
简介:Python3
中也有urllib
和urllib3
两个库,其中urllib
几乎是Python2
中urllib
和urllib2
两个模块的集合,所以我们最常用的urllib
模块,而urllib3
则作为一个拓展模块使用。
urllib发起请求
import urllib
from urllib import request
res = request.urlopen("http://www.baidu.com")
print(res.read())
urllib进行url编码
import urllib
from urllib import parse
d = {"a":"","b":""}
parse.urlencode(d) 结果:'a=1&b=2'
详见:
python内建模块发起HTTP(S)请求的更多相关文章
- Python内建模块--collections
python内建模块--collections collections是Python内建的一个集合模块,提供了许多有用的集合类. namedtuple 我们知道tuple可以表示不变集合,例如,一个点 ...
- python 内建模块与第三方模块
*)datetime模块 包括时间.时间对象.时间戳.时区.时区的转换 参考链接:https://www.liaoxuefeng.com/wiki/1016959663602400/101764878 ...
- python内建模块Collections
# -*- coding:utf-8 -*- # OrderedDict可以实现一个FIFO(先进先出)的dict, # 当容量超出限制时,先删除最早添加的Key: from collections ...
- python——内建模块instance的学习
python中内建函数isinstance的用法 语法:isinstance(object,type) 作用:来判断一个对象是否是一个已知的类型. 其第一个参数(object)为对象,第二个参数(ty ...
- python内建模块——collections模块
在内置数据类型(dict.list.set.tuple)的基础上,collections模块还提供了几个额外的数据类型:Counter.deque.defaultdict.namedtuple和Ord ...
- python的常用内建模块与常用第三方模块
本文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,如有问题请及时联系我们以作处理 一.常用内置模块1.datetimePython 提供了一个 time 和 calendar 模块可 ...
- Python常用内建模块和第三方库
目录 内建模块 1 datetime模块(处理日期和时间的标准库) datetime与timestamp转换 str与datetime转换 datetime时间加减,使用timedelta这个类 转 ...
- Python常用内建模块
Python常用内建模块 datetime 处理日期和时间的标准库. 注意到datetime是模块,datetime模块还包含一个datetime类,通过from datetime import da ...
- python常用内建模块 collections,bs64,struct,hashlib,itertools,contextlib,xml
# 2 collections 是Python内建的一个集合模块,提供了许多有用的集合类. # 2.1 namedtuple #tuple可以表示不变集合,例如,一个点的二维坐标就可以表示成: p ...
随机推荐
- The last packet successfully received from the server was 1,480 milliseconds ago.
场景:一个上传接口,需要上传几十M的文件,文件中包含10几W的数据,然后对10+W的数据进行同步批量插入,每次批量插入1W.最后返回结果. 项目上线一段时间后,上传接口出现问题,数据库用的MySQL5 ...
- svg轻松实现文字水印
1. 水印图片生成采用svg,这样可以运行时生成名字或其他信息的图片 svg模板 <svg xmlns="http://www.w3.org/2000/svg" xmlns: ...
- Java分布式:分布式服务框架——ZooKeeper
Java分布式:ZooKeeper——核心概念 ZooKeeper 统一配置管理 统一命名服务 分布式锁
- 三层交换,单臂路由,vtp
- 修复Nginx报错:upstream sent too big header while reading response header from upstream
在 nginx.conf 的http段,加入下面的配置: proxy_buffer_size 128k; proxy_buffers 32k; proxy_busy_buffers_size 128k ...
- 【layui】获取layui弹窗的index并关闭
var index = parent.layer.getFrameIndex(window.name); //获取窗口索引 parent.layer.close(index); // 关闭当前laye ...
- ACM- 编程练习网站--输入数据方法
#include "stdafx.h" #include <iostream> #include <string> #include <algorit ...
- 将python工程部署到新服务器(对virtualenv工具进行环境迁移)
将python工程部署到新服务器(对virtualenv工具进行环境迁移) # 从开发的电脑上导出 pip list 到 requirements.txt 文件pip freeze > requ ...
- Docker容器安装配置SQLServer服务(Linux)
一:前言 随着不断的对Docker容器的实践和学习,越来越觉得容器的强大,把 SQL Server 数据库服务放在docker容器中,比你自己在宿主服务器上面安装配置一个SQL Server服务器是要 ...
- IDEA界面太丑??尝试一下这几个风格
>>>>>>>>>>原文地址<<<<<<<<<< >>>> ...