1:nginx反向代理(需要两个nginx服务)
在nginx反向代理服务器通过使用proxy_set_header实现。
e. proxy_set_header X-Forwarded-For 127.0.0.1
2:安装模块Passenger
[Passenger用户手册] https://github.com/biti/passenger-doc-zh/wiki/Passenger%E7%94%A8%E6%88%B7%E6%89%8B%E5%86%8Cnginx%E7%89%88
安装完成后使用:
passenger_set_cgi_param HTTP_X_FORWARDED_FOR 127.0.0.1
来增加请求header中的参数
应用场景:
1. 切流,一个线上服务,新增了一个功能,需要公网内部测试,这时候可以根据来源ip &path设置不同的header信息,配合业务逻辑实现内外网流量切分,保证内测功能只有内网可见。
2. 使用非标准标头向上游服务器通知用户的IP地址和其他请求属性
Header可以通过Request提供的.add_header()方法进行添加,示例代码如下:
123456789101112# -*- coding:utf-8 -*-
import urllib2import urlliburl = 'http://ah.example.com'half_url = u'/servlet/av/jd?
ai=782&ji=2624743&sn=I'#构造get请求req = urllib2.
Request(url+half_url.
encode('utf-8'))#添加headerreq.add_header('AcceptEncoding', 'gzip,deflate')req.
add_header('User-Agent','Mozilla/5.0')response = urllib2.
urlopen(req)
print response.
Requests支持流式上传,这允许你发送大的数据流或文件而无需先把它们读入内存。要使用流式上传,仅需为你的请求体提供一个类文件对象即可。
读取文件请使用字节的方式,这样Requests会生成正确的Content-Length。
with open('massive-body', 'rb') as f:
requests.post('http://some.url/streamed', data=f)
分块传输编码
对于出去和进来的请求,Requests也支持分块传输编码。要发送一个块编码的请求,仅需为你的请求体提供一个生成器
注意生成器输出应该为bytes
def gen():
yield b'hi'
yield b'there'
requests.post('http://some.url/chunked', data=gen())
For chunked encoded responses, it's best to iterate over the data
using Response.iter_content(). In an ideal situation you'll have set stream=True on the
request, in which case you can iterate chunk-by-chunk by calling iter_content with a chunk
size parameter of None. If you want to set a maximum size of the chunk, you can set a chunk
size parameter to any integer.
原文: https://blog.csdn.net/sk719887916/article/details/52189602
添加统一通用header, 不会覆盖前面的header
okHttpClient.interceptors().add(new Interceptor() {
@Override
public Response intercept(Interceptor.Chain chain) throws IOException {
Request original = chain.request()
// Request customization: add request headers
Request.Builder requestBuilder = original.newBuilder()
.addHeader("header-key", "value1")
.addHeader("header-key", "value2")
Request request = requestBuilder.build()
return chain.proceed(request)
}
})
添加统一通用header, 会覆盖前面的header
okHttpClient.interceptors().add(new Interceptor() {
@Override
public Response intercept(Interceptor.Chain chain) throws IOException {
Request original = chain.request()
// Request customization: add request headers
Request.Builder requestBuilder = original.newBuilder()
.header("headerkey", "header-value")// <-- this is the important line
Request request = requestBuilder.build()
return chain.proceed(request)
}
})
统一添加参数
OkHttpClient.Builder httpClient =
new OkHttpClient.Builder()
httpClient.addInterceptor(new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
Request original = chain.request()
HttpUrl originalHttpUrl = original.url()
HttpUrl url = originalHttpUrl.newBuilder()
.addQueryParameter("apikey", "your-actual-api-key")
.build()
// Request customization: add request headers
Request.Builder requestBuilder = original.newBuilder()
.url(url)
Request request = requestBuilder.build()
return chain.proceed(request)
}
})
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)