How to perform HTTP GET operation in Python? [clos

2019-09-30 07:59发布

I want my code to take "Request url" as input and gives output as "Response XML". This I want to achieve using python. I don't know how since I'm new to python. Although I know how to do this in Java and for this I already have developed code in Java. So if anyone can help me with this.

Java code snippet:

import java.net.*;    // import java packages.  
import java.io.*;
import java.net.URL;

public class API {
    public static void main(String[] args) throws Exception {
URL API = new URL("http:server//"); // Create a URL object 'API' that will locate the resources on remote server via HTTP protocol.
        URLConnection request = API.openConnection(); // Retrieve a URLConnection object 'request' that will establish http connection by using openConnection() method.
        BufferedReader in = new BufferedReader(new InputStreamReader(
                                    request.getInputStream())); // Create an Output stream ‘in’ that will call InputStreamReader to read the contents of the resources.
        String response;
        while ((response = in.readLine()) != null) // Write to Output stream until null.
            System.out.println(response); // prints the response on Console.
        in.close(); // Close Output stream.
    }
}

标签: python
2条回答
Evening l夕情丶
2楼-- · 2019-09-30 08:42

There is also a nice package called requests that makes your http needs in Python a lot easier.

For a get request you would do:

r = requests.get('http://www.example.com')
print(r.text)
查看更多
ら.Afraid
3楼-- · 2019-09-30 08:43
from socket import *
s = socket()
s.connect(('example.com', 80))
s.send('GET / HTTP/1.1\r\n\r\n')
print s.recv(8192)

?

Or: http://docs.python.org/2/library/urllib2.html

import urllib2
f = urllib2.urlopen('http://www.python.org/')
print f.read(100)



The first option might need more header items, for instance:

from socket import *
s = socket()
s.connect(('example.com', 80))
s.send('GET / HTTP/1.1\r\nHost: example.com\r\nUser-Agent: MyScript\r\n\r\n')
print s.recv(8192)

Also, the first solution which i tend to lean towards (because, you do what you want and nothing else) requires you to have basic understanding of the HTTP protocol.

For instance, this is how the HTTP protocol works for a GET request:

GET <url> HTTP/1.1<cr+lf>
<header-key>: <value><cr+lf>
<cr+lf>

More on this here for instance: http://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol#Client_request

查看更多
登录 后发表回答