环境变量设置代理
全局设置代理就使用环境变量配置(这里只针对Linux或者Mac)
vim /etc/profile
--------------------------------------------
http_proxy=IP:PORT
https_proxy=IP:PORT
export http_proxy
export https_proxy
--------------------------------------------
source /etc/profile
Django发送接口请求配置代理
不配置环境变量,只配置接口使用代理
import requests
import django.http import JsonResponse
def xxxAPIView(requests):
# 接口入参
params = {
'key1':'value1',
'key2':'value2',
...
}
# 代理配置
proxies = {
"http":'http://ip_address:port',
'https':'https://ip_address:port',
}
# Headers配置
token = generate_token() # 生成token的方法,根据实际情况定义
headers = {
'Authorization': 'Bearer '+ token
}
# 初始化会话并更新代理配置
session = requests.Session()
session.proxies.update(proxies)
session.keep_alive = False
# 发送请求
res_get = session.get(url=api_url,json=params,headers=headers)
# res_post = session.get(url=api_url,json=params,headers=headers)
# 将响应转成json格式
result_get.json()
# result_post.json()
return JsonResponse(res_get,safe=False)
标签:http,get,代理,headers,json,session,关于,设置
From: https://www.cnblogs.com/totopian/p/16847861.html