欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

詳解Python 重學(xué)requests發(fā)起請(qǐng)求的基本方式

 更新時(shí)間:2020年02月07日 11:37:38   作者:haeasringnar  
這篇文章主要介紹了詳解Python 重學(xué)requests發(fā)起請(qǐng)求的基本方式,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

安裝相關(guān)模塊

pip install requests requests-toolbelt

代碼實(shí)例

import requests
import json
from PIL import Image
from io import BytesIO
from requests_toolbelt import MultipartEncoder
'''
使用 requests 請(qǐng)求返回的 response 注意事項(xiàng)
response.text 獲得響應(yīng)結(jié)果的字符串類型
response.content 獲得響應(yīng)結(jié)果的bytes(二進(jìn)制數(shù)據(jù)流類型,可用來處理返回的二進(jìn)制文件流) 如果是圖片的話可以使用 Image.open(BytesIO(r.content)).show() 打開查看
response.status_code 獲得響應(yīng)結(jié)果的狀態(tài)碼
response.headers 獲得響應(yīng)結(jié)果的請(qǐng)求頭
response.encoding 獲得響應(yīng)結(jié)果的編碼
response.url 獲得請(qǐng)求的url
response.json() 將獲得響應(yīng)結(jié)果轉(zhuǎn)換成 json.loads(str) 后的結(jié)果,在python中得到字典類型
'''


def get_request(url, params, headers=None):
  '''
  發(fā)起GET請(qǐng)求
  :url 請(qǐng)求的地址 字符串類型
  :params 請(qǐng)求的參數(shù) 字典類型
  :headers 定義請(qǐng)求頭 字典類型
  '''
  return requests.get(url=url, params=params, headers=headers)


def post_www_form_request(url, www_form, headers=None):
  '''
  發(fā)起POST請(qǐng)求 發(fā)送x-www-form-urlencoded請(qǐng)求體
  :url 請(qǐng)求的地址 字符串類型
  :www_form x-www-form-urlencoded請(qǐng)求體 字典類型
  :headers 定義請(qǐng)求頭 字典類型
  '''
  return requests.post(url=url, data=www_form, headers=headers)


def post_form_data_request(url, form_data, headers=None):
  '''
  發(fā)起POST請(qǐng)求 發(fā)送form-data請(qǐng)求體
  :url 請(qǐng)求的地址 字符串類型
  :form_data form-data請(qǐng)求體 字典類型
  :headers 定義請(qǐng)求頭 字典類型
  '''
  default_headers = {'Content-Type': 'multipart/form-data'}
  if headers:
    default_headers.update(headers)
  m = MultipartEncoder(fields=form_data)
  default_headers['Content-Type'] = m.content_type
  print(default_headers)
  return requests.post(url=url, data=m, headers=default_headers)


def post_json_data_request(url, json_data, headers=None):
  '''
  發(fā)起POST請(qǐng)求 發(fā)送json請(qǐng)求體
  :url 請(qǐng)求的地址 字符串類型
  :json_data json類型請(qǐng)求體 字典類型
  :headers 定義請(qǐng)求頭 字典類型
  '''
  # 方式一
  # default_headers = {'Content-Type': 'application/json'}
  # if headers:
  #   default_headers.update(headers)
  # return requests.post(url=url, data=json.dumps(json_data), headers=default_headers)
  # 方式二
  return requests.post(url=url, json=json_data, headers=headers)


def post_files_request(url, files, headers=None):
  '''
  發(fā)起POST請(qǐng)求 請(qǐng)求體為文件
  :url 請(qǐng)求的地址 字符串類型
  :files 文件類型請(qǐng)求體 文件類型
  :headers 定義請(qǐng)求頭 字典類型
  '''
  # 攜帶請(qǐng)求頭
  default_headers = {'Authorization': 'bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpZCI6MSwiZXhwIjoxMDIxNTk5MjgwMX0.GFs_smaKQ55taYgctbDzw2ooOdKNuy-HqobHXB2nE1o'}
  if headers:
    default_headers.update(headers)
  return requests.post(url=url, files=files, headers=default_headers)


if __name__ == '__main__':
  # 測(cè)試GET請(qǐng)求
  # print(get_request('http://127.0.0.1:9000/wechat/good/', {'page': 1, 'page_size': 2}).json())
  # print(post_www_form_request('http://127.0.0.1:9000/mobilelogin/', {'mobile': '17316280277', 'code': '1234'}).json())
  # (('mobile', '17316280277'), ('code', '1234'))
  # print(post_form_data_request('http://127.0.0.1:9000/mobilelogin/', {'mobile': '17316280277', 'code': '1234'}).json())
  # print(post_json_data_request('http://127.0.0.1:9000/mobilelogin/', {'mobile': '17316280277', 'code': '1234'}).json())
  print(post_files_request('http://127.0.0.1:9000/uploadfile/', {'file': open('img1.png', 'rb'), 'file1': open('1.xls', 'rb')}).json())

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論