python使用get和post方法_在Python3中使用urllib实现http的get和post提交数据操作

python是一种面向对象、解释型计算机程序设计语言,由guido van rossum于1989年底发明,第一个公开发行版发行于1991年。python语法简洁而清晰,具有丰富和强大的类库。它常被昵称为胶水语言,它能够把用其他语言制作的各种模块(尤其是c/c++)很轻松地联结在一起。常见的一种应用情形是,使用python快速生成程序的原型(有时甚至是程序的最终界面),然后对其中有特别要求的部分,用更合适的语言改写,比如3d游戏中的图形渲染模块,性能要求特别高,就可以用c++重写。

在Python3中使用urllib实现http的get操作的核心部分代码如下:url="http://www.169it.com"

header_dict={'User-Agent':

'Mozilla/5.0 (Windows NT 6.1; Trident/7.0; rv:11.0) like Gecko'}

req = Request(url=url,headers=header_dict)

f = urlopen(req,timeout=120)

在Python3中使用urllib实现http的post操作的核心部分代码如下:url="http://www.169it.com"

header_dict={'User-Agent':

'Mozilla/5.0 (Windows NT 6.1; Trident/7.0; rv:11.0) like Gecko'}

#pdata为post内容,为dict类型

tmp_pdata=urllib.parse.urlencode(pdata)

req = Request(url=url,

data=tmp_pdata.encode(encoding="utf-8",errors="ignore"),

headers=header_dict,method='POST')

f = urlopen(req,timeout=120)

Python3中使用urllib实现http的 GET 方法>>> import httplib

>>> conn = httplib.HTTPConnection("www.python.org")

>>> conn.request("GET", "/index.html")

>>> r1 = conn.getresponse()

>>> print r1.status, r1.reason

200 OK

>>> data1 = r1.read()

>>> conn.request("GET", "/parrot.spam")

>>> r2 = conn.getresponse()

>>> print r2.status, r2.reason

404 Not Found

>>> data2 = r2.read()

>>> conn.close()

Python3中使用urllib实现http的HEAD 方法>>> import httplib

>>> conn = httplib.HTTPConnection("www.python.org")

>>> conn.request("HEAD","/index.html")

>>> res = conn.getresponse()

>>> print res.status, res.reason

200 OK

>>> data = res.read()

>>> print len(data)

0

>>> data == ''

True

Python3中使用urllib实现http的 POST 方法>>> import httplib, urllib

>>> params = urllib.urlencode({'spam': 1, 'eggs': 2, 'bacon': 0})

>>> headers = {"Content-type": "application/x-www-form-urlencoded",

...            "Accept": "text/plain"}

>>> conn = httplib.HTTPConnection("musi-cal.mojam.com:80")

>>> conn.request("POST", "/cgi-bin/query", params, headers)

>>> response = conn.getresponse()

>>> print response.status, response.reason

200 OK

>>> data = response.read()

>>> conn.close()

Python,有几种常用的库可以用来发送HTTP请求,包括内置的`urllib`库,第三方库`urllib3`以及流行的`requests`库。这里我会简单介绍这三种库如何分别使用`GET`和`POST`请求: 1. **urllib库**: - GET请求示例(urlopen函数): ```python import urllib.request url = 'http://example.com/api' response = urllib.request.urlopen(url) data = response.read() ``` - POST请求示例(urlopen配合data=...参数): ```python from urllib.parse import urlencode data = {'key': 'value'} params = urlencode(data).encode('utf-8') request = urllib.request.Request(url, params) with urllib.request.urlopen(request) as response: response_data = response.read() ``` 2. **urllib3库**: - GET请求示例(`requests.get`等效): ```python import urllib3 http = urllib3.PoolManager() response = http.request('GET', 'http://example.com/api') data = response.data ``` - POST请求示例: ```python http = urllib3.PoolManager() post_data = { 'key': 'value' } response = http.request('POST', 'http://example.com/api', fields=post_data) response_data = response.data ``` 3. **requests库**: - GET请求示例(简洁易用): ```python import requests response = requests.get('http://example.com/api') data = response.text ``` - POST请求示例: ```python response = requests.post('http://example.com/api', data={'key': 'value'}) response_data = response.json() # 如果返回的是JSON数据 ``` 以上三种库各有特点,`requests`因为其更友好的API和丰富的功能常被推荐给新手和实际项目使用。然而,如果对性能优化或底层操作有特殊需求,`urllib`和`urllib3`会是更好的选择。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值