小编Aur*_*ert的帖子

具有超时,最大大小和连接池的http请求

我正在寻找Python(2.7)中的一种方法来执行具有3个要求的HTTP请求:

  • 超时(可靠性)
  • 内容最大尺寸(安全性)
  • 连接池(用于性能)

我已经检查了所有python HTTP库,但它们都不符合我的要求.例如:

urllib2:很好,但没有汇集

import urllib2
import json

r = urllib2.urlopen('https://github.com/timeline.json', timeout=5)
content = r.read(100+1)
if len(content) > 100: 
    print 'too large'
    r.close()
else:
    print json.loads(content)

r = urllib2.urlopen('https://github.com/timeline.json', timeout=5)
content = r.read(100000+1)
if len(content) > 100000: 
    print 'too large'
    r.close()
else:
    print json.loads(content)
Run Code Online (Sandbox Code Playgroud)

请求:没有最大尺寸

import requests
r = requests.get('https://github.com/timeline.json', timeout=5, stream=True)
r.headers['content-length'] # does not exists for this request, and not safe
content = r.raw.read(100000+1)
print content # ARF this is gzipped, so not the real …
Run Code Online (Sandbox Code Playgroud)

python timeout connection-pooling http max-size

8
推荐指数
1
解决办法
1万
查看次数

如何知道2张地图是否引用了相同的数据

Go 映射是对内部数据的引用。这意味着当“复制”地图时,它们最终会共享相同的参考并因此编辑相同的数据。这与使用具有相同项目的另一张地图大不相同。但是,我找不到任何方法来区分这两种情况。

import "fmt"
import "reflect"

func main() {
    a := map[string]string{"a": "a", "b": "b"}
    // b references the same data as a
    b := a
    // thus editing b also edits a
    b["c"] = "c"
    // c is a different map, but with same items
    c := map[string]string{"a": "a", "b": "b", "c": "c"}

    reflect.DeepEqual(a, b) // true
    reflect.DeepEqual(a, c) // true too
    a == b // illegal
    a == c // illegal too
    &a == &b // false
    &a …
Run Code Online (Sandbox Code Playgroud)

go go-map

2
推荐指数
1
解决办法
198
查看次数

标签 统计

connection-pooling ×1

go ×1

go-map ×1

http ×1

max-size ×1

python ×1

timeout ×1