How to perform HTTP GET operation in Python? [closed]

2024/11/7 21:17:38

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.}
}
Answer
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

https://en.xdnf.cn/q/120696.html

Related Q&A

Python Error TypeError: cannot concatenate str and float objects [duplicate]

This question already has answers here:Making a string out of a string and an integer in Python [duplicate](5 answers)Closed 7 years ago.I am new with Python programming. I keep getting the below error…

Countif function in python

enter image description here In excel file i can do countif funtion like attached picture but How can i do this countif function in Python Pandas,please help me by providing the code

How do i implement these algorithms below

Alogrithm 1:Get a list of numbers L1, L2, L3....LN as argumentAssume L1 is the largest, Largest = L1Take next number Li from the list and do the followingIf Largest is less than LiLargest = LiIf Li is …

How to run a shell script once a day?

I am trying to run this particular shell script only one time, daily. Heres my code for runLucene.py:#!/usr/bin/env pythonimport os from extras.download_datos_desambiguar import news_Lucenex=datetime.t…

How to fix Error: Please select a valid Python interpreter in Pycharm?

Error:Error: Please select a valid Python interpreterScreenshot:How to fix this?

Tuples conversion into JSON with python [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.Want to improve this question? Add details and clarify the problem by editing this post.Closed 8 years ago.Improve…

Python continue with while [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.This question was caused by a typo or a problem that can no longer be reproduced. While similar q…

Create MySQL database with python [closed]

Closed. This question needs debugging details. It is not currently accepting answers.Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to repro…

Basic Python repetition exercise

I was trying to learn Python when I came upon this question. I am not asking for you to answer the question for me, I just need some help. Note: I am only allowed to use loops and if statements etc. No…

Python 3.3 socket programming error [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.Questions asking for code must demonstrate a minimal understanding of the problem being solved. Incl…