我正在寻找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) 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)