Explore Courses Blog Tutorials Interview Questions
0 votes
in Python by (1.6k points)

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(' ');

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!

1 Answer

0 votes
by (25.1k points)

You can use the urllib module's request method. NOTE that in Python 2 you need to use urllib2 module.

Python 3:

import urllib.request

contents = urllib.request.urlopen("").read()

Python 2:

import urllib2

contents = urllib2.urlopen("").read()

Related questions

0 votes
1 answer
0 votes
1 answer
asked Jul 24, 2019 in Java by Nigam (4k points)
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
asked Oct 9, 2019 in Python by Sammy (47.6k points)
Welcome to Intellipaat Community. Get your technical queries answered by top developers!

29.3k questions

30.6k answers


104k users

Browse Categories