Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
170 views
in Technique[技术] by (71.8m points)

python - 在Python中最快的HTTP GET方法是什么?(What is the quickest way to HTTP GET in Python?)

What is the quickest way to HTTP GET in Python if I know the content will be a string?

(如果我知道内容将是字符串,那么用Python进行HTTP GET的最快方法是什么?)

I am searching the documentation for a quick one-liner like:

(我正在搜索文档,以查找像以下这样的快速单行代码:)

contents = url.get("http://example.com/foo/bar")

But all I can find using Google are httplib and urllib - and I am unable to find a shortcut in those libraries.

(但是我可以使用Google找到的只是httpliburllib我无法在这些库中找到快捷方式。)

Does standard Python 2.5 have a shortcut in some form as above, or should I write a function url_get ?

(标准Python 2.5是否具有上述某种形式的快捷方式,还是应该编写url_get函数?)

  1. I would prefer not to capture the output of shelling out to wget or curl .

    (我宁愿不要捕获脱壳输出到wgetcurl的输出。)

  ask by Frank Krueger translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Python 3:

(Python 3:)

import urllib.request
contents = urllib.request.urlopen("http://example.com/foo/bar").read()

Python 2:

(Python 2:)

import urllib2
contents = urllib2.urlopen("http://example.com/foo/bar").read()

Documentation for urllib.request and read .

(urllib.requestread文档。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...