[python] How do you send an HTTP Get Web Request in Python?

I am having trouble sending data to a website and getting a response in Python. I have seen similar questions, but none of them seem to accomplish what I am aiming for.

This is my C# code I'm trying to port to Python:

    static void Request(Uri selectedUri)
    {
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(selectedUri);
        request.ServicePoint.BindIPEndPointDelegate = BindIPEndPointCallback;
        request.Method = "GET";
        request.Timeout = (int)Timeout.TotalMilliseconds;
        request.ReadWriteTimeout = (int)Timeout.TotalMilliseconds;
        request.CachePolicy = CachePolicy;
        request.UserAgent = UserAgent;

        using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
        {
            using (StreamReader responseReader = new StreamReader(response.GetResponseStream()))
            {
                string responseText = responseReader.ReadToEnd();
                File.WriteAllText(UrlFileName, responseText.Trim(), Encoding.ASCII);
            }
        }
     }

Here is my attempt in Python:

def request():
web = httplib.HTTPConnection('https://someurl.com');
headers = {"Content-type": "application/x-www-form-urlencoded", "Accept": "text/plain"}
web.request("GET", "/heartbeat.jsp", headers);
response = web.getresponse();
stream = ""; #something is wrong here

Any help would be appreciated!

This question is related to python http streamreader webrequest

The answer is


You can use urllib2

import urllib2
content = urllib2.urlopen(some_url).read()
print content

Also you can use httplib

import httplib
conn = httplib.HTTPConnection("www.python.org")
conn.request("HEAD","/index.html")
res = conn.getresponse()
print res.status, res.reason
# Result:
200 OK

or the requests library

import requests
r = requests.get('https://api.github.com/user', auth=('user', 'pass'))
r.status_code
# Result:
200

In Python, you can use urllib2 (http://docs.python.org/2/library/urllib2.html) to do all of that work for you.

Simply enough:

import urllib2
f =  urllib2.urlopen(url)
print f.read() 

Will print the received HTTP response.

To pass GET/POST parameters the urllib.urlencode() function can be used. For more information, you can refer to the Official Urllib2 Tutorial


Examples related to python

programming a servo thru a barometer Is there a way to view two blocks of code from the same file simultaneously in Sublime Text? python variable NameError Why my regexp for hyphenated words doesn't work? Comparing a variable with a string python not working when redirecting from bash script is it possible to add colors to python output? Get Public URL for File - Google Cloud Storage - App Engine (Python) Real time face detection OpenCV, Python xlrd.biffh.XLRDError: Excel xlsx file; not supported Could not load dynamic library 'cudart64_101.dll' on tensorflow CPU-only installation

Examples related to http

Access blocked by CORS policy: Response to preflight request doesn't pass access control check Axios Delete request with body and headers? Read response headers from API response - Angular 5 + TypeScript Android 8: Cleartext HTTP traffic not permitted Angular 4 HttpClient Query Parameters Load json from local file with http.get() in angular 2 Angular 2: How to access an HTTP response body? What is HTTP "Host" header? Golang read request body Angular 2 - Checking for server errors from subscribe

Examples related to streamreader

Converting file into Base64String and back again How do you send an HTTP Get Web Request in Python? How to Find And Replace Text In A File With C# Should I call Close() or Dispose() for stream objects? How to read embedded resource text file Reading large text files with streams in C#

Examples related to webrequest

Which versions of SSL/TLS does System.Net.WebRequest support? How do you send an HTTP Get Web Request in Python? Reasons for a 409/Conflict HTTP error when uploading a file to sharepoint using a .NET WebRequest? HttpWebRequest using Basic authentication Cannot send a content-body with this verb-type How to use WebRequest to POST some data and read response? How to get json response using system.net.webrequest in c#? How do I use WebRequest to access an SSL encrypted site using https? Cannot set some HTTP headers when using System.Net.WebRequest