patternpythonCritical
What is the quickest way to HTTP GET in Python?
Viewed 0 times
quickestthegetpythonwayhttpwhat
Problem
What is the quickest way to HTTP GET in Python if I know the content will be a string? I am searching the documentation for a quick one-liner like:
But all I can find using Google are
Does standard Python 2.5 have a shortcut in some form as above, or should I write a function
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.Does standard Python 2.5 have a shortcut in some form as above, or should I write a function
url_get?- I would prefer not to capture the output of shelling out to
wgetorcurl.
Solution
Python 3:
Python 2:
Documentation for
import urllib.request
contents = urllib.request.urlopen("http://example.com/foo/bar").read()Python 2:
import urllib2
contents = urllib2.urlopen("http://example.com/foo/bar").read()Documentation for
urllib.request and read.Code Snippets
import urllib.request
contents = urllib.request.urlopen("http://example.com/foo/bar").read()import urllib2
contents = urllib2.urlopen("http://example.com/foo/bar").read()Context
Stack Overflow Q#645312, score: 1033
Revisions (0)
No revisions yet.