I'm sorry to have used the resources with the material full of feelings now, Mr. Qiit.
It is necessary to hit while changing the Web API in the business system of the company, so I thought that I would try it with Python. I did some communication using the socket object before, so I thought it would be somehow.
So, while making an HTTP client based on socket, I tried various gugrecas and found that it seems different. Upon further investigation, I came to the conclusion that it would be better to use requests.
So, for the time being, I made something that I could just get.
In this case, it is mostly described in the above document.
requests need to be installed with pip. In my case, I checked with Cygwin (32bit) of Windows 10.
The information on this site was very helpful. Thank you, tanao.
Looking at the above site, it seems that there were various handling around pip in Cygwin, but as of July 18, 2016, it seems that the following can be done if the environment is Windows10 / Cygwin32bit, Python2.7.
pip install requests
.For the time being, I tried to make it a class with a pitiful reason to remember how to write the class.
import requests
class SampleHTTPclient:
def __init__(self, url):
self.url = url
self.protocol = ""
self.error_message = ""
self.request_headers = None
self.response_headers = None
self.status_code = None
def check_protocol(self):
# ex
# if self.protocol = "http://" , self.uri = "hoge.org":
# self.url = "http://hoge.org"
if self.url.find("://") == -1 and len(self.protocol) > 3:
self.url = self.protocol + self.url
def set_protocol(self, protocol):
self.protocol = protocol
self.check_protocol()
def set_headers(self, headers):
self.request_headers = headers
def get(self, fp):
try:
if self.request_headers is None:
response = requests.get(self.url)
else:
response = requests.get(self.url, headers=self.request_headers)
fp.write(response.content)
except Exception as ex:
self.error_message = str(ex)
return False
else:
self.status_code = response.status_code
self.response_headers = response.headers
return True
def get_code(self):
return self.status_code
def get_headers(self):
return self.response_headers
def get_error_message(self):
return self.error_message
The entity is two lines, but ... I've messed it up. ..
The usage is the following image.
1 There is no problem even if you specify the protocol character string with set_protcol after generating the SampleHTTPclient by specifying the URL character string including the protocol such as http: //. In this case, the set_protocol setting is ignored.
2 For example, an HTTP 404 error is treated as a success in terms of the get function. Therefore, please check the communication result with get_code etc. At present, the specifications are such that the body is written to a file even in this case. If that is inconvenient, we apologize for the inconvenience, but please remodel it.
Here is an example of using SampleHTTPclient with the above description coded.
print "sample httpc program."
url = "<<The URL of the resource to get here>>"
headers = {"X-gdaigo_para1": "fujiko", "X-gdaigo-para2":"7890"}
filename = "test.html"
print url, " -> " , filename
httpc = SampleHTTPclient(url);
httpc.set_protocol("http://")
httpc.set_headers(headers)
fp = open(filename, "w")
success = httpc.get(fp)
fp.close()
if success == True:
headers = httpc.get_headers()
print "response code=", httpc.get_code()
for key, value in headers.iteritems():
print key, ":" , value
print "success."
else:
print "httpc:read error\n" , httpc.get_error_message()
print "error."
print "complete."
The above example is an example of acquiring resources based on the URL and acquiring the data as test.html. The header is set (although it is meaningless), and the acquisition result is also displayed. GET seems to be okay like this.
So I was able to do it relatively easily by using requests.
I used it below. Thank you for providing the wonderful software.
that's all.
Recommended Posts