Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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!

like image 797
voice Avatar asked Jun 18 '13 20:06

voice


People also ask

How do you send and receive HTTP requests in Python?

These ones are quite easy to use, and lightweight. For example, a small client-side code to send the post variable foo using requests would look like this: import requests r = requests. post("http://yoururl/post", data={'foo': 'bar'}) # And done.

What is the function of get in HTTP requests in Python?

The GET method is used to retrieve information from the given server using a given URI. Requests using GET should only retrieve data and should have no other effect on the data.


1 Answers

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 
like image 86
Victor Castillo Torres Avatar answered Oct 13 '22 05:10

Victor Castillo Torres