When using the HTTPCollection module of the http.client module, while reading the contents of the site, an error 301 appears. The site itself works fine in the browser, when using the urlopen function everything works. I understand that the solution here is obvious: just use urlopen, but still I would like to know why this is so?

Here is the code:

from http.client import HTTPConnection privat = HTTPConnection('api.privatbank.ua', 80) privat.request('GET', '/p24api/pubinfo?json&exchange&coursid=5') result = privat.getresponse() print(result.read()) # выводит это: b'<html>\r\n<head><title>301 Moved Permanently</title></head>\r\n<body bgcolor="white">\r\n<center><h1>301 Moved Permanently</h1></center>\r\n<hr><center>nginx</center>\r\n</body>\r\n</html>\r\n' 

Here is the working code:

 import urllib.request, json with urllib.request.urlopen("http://api.privatbank.ua/p24api/pubinfo?json&exchange&coursid=5") as url: data = json.loads(url.read().decode()) print(data) #выводит это: [{'ccy': 'USD', 'base_ccy': 'UAH', 'buy': '26.70000', 'sale': '26.95000'}, {'ccy': 'EUR', 'base_ccy': 'UAH', 'buy': '30.20000', 'sale': '30.70000'}, {'ccy': 'RUR', 'base_ccy': 'UAH', 'buy': '0.38700', 'sale': '0.41200'}, {'ccy': 'BTC', 'base_ccy': 'USD', 'buy': '3551.4864', 'sale': '3925.3270'}] 

I can’t understand why in the first code there is an error 301(b'<html>\r\n<head><title>301 Moved Permanently</title></head>\r\n<body bgcolor="white">\r\n<center><h1>301 Moved Permanently</h1></center>\r\n<hr><center>nginx</center>\r\n</body>\r\n</html>\r\n') , and in the second everything works fine

  • What error gives, here simply all have access to your computer - michael_best
  • how what? - michael_best
  • yes ... you can see my humor in the evening rolls over. sorry - michael_best
  • well well. All code from comments - up. and I do not really understand your problem - michael_best pm
  • you have two pieces of code: 1 some, and something else. Clearly state what you want to hear from the community. "Explain" - you will most likely be sent to read a tutorial - michael_best

2 answers 2

301 Moved Permanently indicates that the page address has changed and the Location header usually indicates which one.

Let's check it out.

 from http.client import HTTPConnection privat = HTTPConnection('api.privatbank.ua') privat.request('GET', '/p24api/pubinfo?json&exchange&coursid=5') result = privat.getresponse() print(result.getheader('Location')) # https://api.privatbank.ua/p24api/pubinfo?json&exchange&coursid=5 

As you can see, the address has really changed - from http to https . Here is such a redirect.

And what about urllib ? And here is what - he himself made a redirect:

 import urllib.request with urllib.request.urlopen("http://api.privatbank.ua/p24api/pubinfo?json&exchange&coursid=5") as rs: print(rs.url) # https://api.privatbank.ua/p24api/pubinfo?json&exchange&coursid=5 

Ps.

It seems that for http.client it will be necessary to check the status value in the response and for 301 to pull out a new address and download it. Or immediately use HTTPSConnection :

 from http.client import HTTPSConnection privat = HTTPSConnection('api.privatbank.ua') privat.request('GET', '/p24api/pubinfo?json&exchange&coursid=5') result = privat.getresponse() print(result.read()) # b'[{"ccy":"USD","base_ccy":"UAH","buy":"26.70000","sale":"26.95000"},{"ccy":"EUR","bas ... 

but, imkho, it is simpler to use urlopen .

  • I think he swears because he needs to specify the host / ip ( see in the dock ), and the protocol makes an invalid argument. The fact that you want to use HTTP or HTTPS follows from the class name HTTPConnection and HTTPSConnection, respectively. Kst, @ ice1x case says - you can immediately use HTTPSConnection, then it should work - just remove the port - for https the default will be 443, but you can not specify it in the constructor - gil9red
  • thank you very much, it helped me to figure everything out - Stas

Use HTTPSConnection instead of HTTPConnection , port 443 instead of port 80, and after result.read()