(一)python发送HTTP 请求的两种方式(get和post )

引入 requests 包

python 复制代码
import requests

注:发送请求(包括请求行、方法类型、头、体) & 常见的请求方式有get、post、put、delete

一、发送get请求

格式:requests.get() (内容: url必填; params选填:url参数字典)

# ~ 无参数的get请求
python 复制代码
res = requests.get(url='http://ws.webxml.com.cn/WebServices/WeatherWS.asmx/getRegionProvince')
print(res.text)
#打印响应主体内容,字符串格式
# ~ 有参数的get请求
python 复制代码
res = requests.get(url='http://ws.webxml.com.cn/WebServices/WeatherWS.asmx/getSupportCityString?theRegionCode=3113')
print(res.text)
# ~ 使用params的get请求
python 复制代码
res = requests.get(url='http://ws.webxml.com.cn/WebServices/WeatherWS.asmx/getSupportCityString',
                  params={"theRegionCode":3113})
print(res.text)

二、发送post请求

知识扩展# requests.post() post请求分为5种,常用的有三种,如下

①application/x-www-form-urlencod (form表单);

②raw (纯文本格式):有5种格式,分别为json/xml/Html/Text/JavaScrip

③multipart/form-data (复合式表单)

# ~ 无参数的post请求

~ 有正文体的post请求

1. form表单(application/x-www-form-urlencod)

-----data 后跟字典

eg.1: 带参数的查询接口

python 复制代码
res = requests.post(url='http://ws.webxml.com.cn/WebServices/WeatherWS.asmx/getSupportCityString',
              headers={"Content-Type": "application/x-www-form-urlencoded"},
              data={"theRegionCode": 3113})
print(res.text)

eg.2: 带账号名密码的登录接口

python 复制代码
res = requests.post(url='http://123.56.99.53:9000/event/api/admin/',
              headers={"Content-Type": "application/x-www-form-urlencoded"},
              data={"username": "admin", "password": "MTIzYWRtaW4="})
print(res.text)

2. 纯文本格式(raw)

**# ~ xml格式 ----**data 后跟字符串

python 复制代码
res = requests.post(url='http://ws.webxml.com.cn/WebServices/WeatherWS.asmx',
              headers ={"Content-Type": "text/xml; charset=utf-8",
                       "SOAPAction": "http://WebXml.com.cn/getSupportCityString"},
              data = '''<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <getSupportCityString xmlns="http://WebXml.com.cn/">
      <theRegionCode>string</theRegionCode>
    </getSupportCityString>
  </soap:Body>
</soap:Envelope>''')
print(res.text)

# ~json格式 ----- json 后跟字典(常用), or ----data 后跟json字符串

python 复制代码
res = requests.post(url='http://123.56.99.53:5000/event/weather/getWeather/',
             headers={"Content-Type": "application/json"},
             json={"theCityCode": "1001"})
print(res.text)

注:# json后面跟的是字典即json={"theCityCode": "1001"}),可改成 data='{"theCityCode": "1001"})';字典能自动转为json字符串,所以一般用字典表示;建议纯json正文的接口使用第一种方式,除非是较长的字符串

3. 复合式表单(multipart/form-data )

------上传二进制文件 ---- key value (接口名称:文件名)

python 复制代码
res = requests.post(url='http://123.56.99.53:9001/api/uploadFile/',
              headers={"Cookie": "uid=1;token=44c972f05d76fdd93c31f9c2b65bb098f308cdfc"
                      #"Content-Type": "multipart/form-data"},
#有的接口不需要写正文体格式,老服务器一般需要写
              files={"myfile1": open('D:\全力以富\1.docx', 'rb')
                     #"myfile2: open(上传多个文件)"})
print(res.text)

注:上传接口 files={'文件路径','rb'} rb表示

相关推荐
Blurpath2 小时前
免费代理IP服务有哪些隐患?如何安全使用?
网络·安全·ip代理·住宅ip
Easybr指纹浏览器3 小时前
5 easybr指纹浏览器内存修改教程
chrome·http·游戏·web3·社媒营销·电商·多账号管理·指纹浏览器·浏览器开发·sock5·跨境营销·账号防关联
Douglassssssss3 小时前
【深度学习】使用块的网络(VGG)
网络·人工智能·深度学习
python算法(魔法师版)4 小时前
网络编程入门(一)
大数据·网络·网络协议·计算机网络
网络小白不怕黑4 小时前
Python Socket编程:实现简单的客户端-服务器通信
服务器·网络·python
Ronin-Lotus4 小时前
程序代码篇---python获取http界面上按钮或者数据输入
python·http
君鼎7 小时前
muduo库TcpServer模块详解
linux·网络·c++
开***能7 小时前
包装设备跨系统兼容:Profinet转Modbus TCP的热收缩包装机改造方案
服务器·网络·tcp/ip
zyp2468107 小时前
深入理解TCP与UDP:协议对比、头部结构与连接管理
网络协议·tcp/ip·udp
卡戎-caryon7 小时前
【MySQL】02.数据库基础
linux·网络·数据库·mysql·存储引擎