Skip to content
Fix Code Error

What is the quickest way to HTTP GET in Python?

March 13, 2021 by Code Error
Posted By: Frank Krueger

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:

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?

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

Solution

Python 3:

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.

Answered By: Nick Presta

Related Articles

  • Download file from web in Python 3
  • List of Timezone IDs for use with FindTimeZoneById() in C#?
  • Downloading Java JDK on Linux via wget is shown license page…
  • What is the incentive for curl to release the library for…
  • HTTPS connection Python
  • GLYPHICONS - bootstrap icon font hex value
  • Does moment.js allow me to derive a timezone abbreviation…
  • How do I measure request and response times at once using…
  • curl -GET and -X GET
  • Adding Dynamic Input Fields With VueJs

Disclaimer: This content is shared under creative common license cc-by-sa 3.0. It is generated from StackExchange Website Network.

Post navigation

Previous Post:

How to get all possible combinations of a list’s elements?

Next Post:

Check if Cell value exists in Column, and then get the value of the NEXT Cell

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

  • Get code errors & solutions at akashmittal.com
© 2022 Fix Code Error