Issue
I have created a program that parses the famous API
from 2ip. The problem arises even when, then the receiving argument was assigned both bytes
and str
, nothing helps, here is the code:
View_dict = {
'View':{
'txt': [
'text', 'txt', 'log', 'TXT', 'LOG'
],
'str': [
'str', 'STR', 'out'
]
}
}
class Main:
class BreakHost:
domain = bytes
mode = str
def __init__(self, Mode, Domain, load_proxy=bytes):
self.domain=Domain
self.mode=Mode
try:
for k, v in View_dict["View"].items():
if Mode in v:
Mode = k
send_requests = requests.get(f'https://api.2ip.ua/hosting.json?site={Domain}', proxies=load_proxy)
answer = send_requests
soup_check = bs4.BeautifulSoup(answer, 'lxml')
error_send = soup_check.find('div', class_='error').text.strip()
if error_send == 'IP address is not valid / IP-адрес не действительный':
if Mode == 'txt':
_error_(View=Mode, TEXT='This is Domain does not exist', NickName=Root, Sender='BreakHost', TypeError='CRITICAL', TypeMSG='Message')
return Sos1skaError(f'Domain not exist, check the entered domain ---> {Domain}')
elif Mode == 'str':
_error_(View=Mode, TEXT='This is Domain does not exist', NickName=Root, Sender='BreakHost', TypeError='CRITICAL', TypeMSG='Message')
return Sos1skaError(f'Domain not exist, check the entered doamin ---> {Domain}')
else:
pass
soup_json = bs4.BeautifulSoup(send_requests).text.strip()
site_json = json.loads(soup_json)
Handler = site_json
if Mode == 'str':
try:
_info_(View='str', TEXT='%s' % (Handler["name_ripe"]), NickName=Root)
except KeyError:
_info_(View='str', TEXT='Not Found Information', NickName=Root)
try:
_info_(View='str', TEXT='%s' % (Handler["name_rus"]), NickName=Root)
except KeyError:
_info_(View='str', TEXT='Not Found Information', NickName=Root)
except KeyboardInterrupt:
_warning_(View='str', TEXT='KeyboardInterrupt', NickName=Root, TypeMSG='Message')
time.sleep(2)
_error_(View='str', TEXT='KeyboardInterrupt', NickName=Root, Sender='BreakHost', TypeError='WARNING', TypeMSG='Message')
return Sos1skaKeyboardInterrupt('Keyboardinterrupt, pressed "CTRL+C"')
P.S. I did not find a solution to the problem on the Internet
Full Error:
File "d:\Development\S_B_FrameWork\__init__.py", line 2, in <module>
Main.BreakHost(Mode='str', Domain='google.com')
File "d:\Development\S_B_FrameWork\services\__init__.py", line 65, in __init__
send_requests = requests.get(f'https://api.2ip.ua/hosting.json?site={Domain}', proxies=load_proxy)
File "C:\Users\Kolia\AppData\Local\Programs\Python\Python39\lib\site-packages\requests\api.py", line 75, in get
return request('get', url, params=params, **kwargs)
File "C:\Users\Kolia\AppData\Local\Programs\Python\Python39\lib\site-packages\requests\api.py", line 61, in request
return session.request(method=method, url=url, **kwargs)
File "C:\Users\Kolia\AppData\Local\Programs\Python\Python39\lib\site-packages\requests\sessions.py", line 532, in request
settings = self.merge_environment_settings(
File "C:\Users\Kolia\AppData\Local\Programs\Python\Python39\lib\site-packages\requests\sessions.py", line 710, in merge_environment_settings
no_proxy = proxies.get('no_proxy') if proxies is not None else None
AttributeError: type object 'bytes' has no attribute 'get'
Solution
class Main:
class BreakHost:
domain = bytes
mode = str
def __init__(self, Mode, Domain, load_proxy=None):
self.domain=Domain
self.mode=Mode
try:
for k, v in View_dict["View"].items():
if Mode in v:
Mode = k
if load_proxy == None:
send_requests = requests.get(f'https://api.2ip.ua/hosting.json?site={Domain}')
answer = send_requests
soup_check = bs4.BeautifulSoup(answer.text, 'html.parser')
error_send = soup_check.find('div', class_='error')
if error_send == 'IP address is not valid / IP-адрес не действительный':
if Mode == 'txt':
_error_(View=Mode, TEXT='This is Domain does not exist', NickName=Root, Sender='BreakHost', TypeError='CRITICAL', TypeMSG='Message')
return Sos1skaError(f'Domain not exist, check the entered domain ---> {Domain}')
elif Mode == 'str':
_error_(View=Mode, TEXT='This is Domain does not exist', NickName=Root, Sender='BreakHost', TypeError='CRITICAL', TypeMSG='Message')
return Sos1skaError(f'Domain not exist, check the entered doamin ---> {Domain}')
else:
pass
soup_json = bs4.BeautifulSoup(send_requests.text, 'html.parser').text
site_json = json.loads(soup_json)
Handler = site_json
if Mode == 'str':
try:
_info_(View='str', TEXT='%s' % (Handler["name_ripe"]), NickName=Root)
except KeyError:
_info_(View='str', TEXT='Not Found Information', NickName=Root)
try:
_info_(View='str', TEXT='%s' % (Handler["name_rus"]), NickName=Root)
except KeyError:
_info_(View='str', TEXT='Not Found Information', NickName=Root)
else:
send_requests = requests.get(f'https://api.2ip.ua/hosting.json?site={Domain}', proxies=load_proxy)
answer = send_requests
soup_check = bs4.BeautifulSoup(answer.text, 'html.parser')
error_send = soup_check.find('div', class_='error')
if error_send == 'IP address is not valid / IP-адрес не действительный':
if Mode == 'txt':
_error_(View=Mode, TEXT='This is Domain does not exist', NickName=Root, Sender='BreakHost', TypeError='CRITICAL', TypeMSG='Message')
return Sos1skaError(f'Domain not exist, check the entered domain ---> {Domain}')
elif Mode == 'str':
_error_(View=Mode, TEXT='This is Domain does not exist', NickName=Root, Sender='BreakHost', TypeError='CRITICAL', TypeMSG='Message')
return Sos1skaError(f'Domain not exist, check the entered doamin ---> {Domain}')
else:
pass
soup_json = bs4.BeautifulSoup(send_requests.text, 'html.parser').text
site_json = json.loads(soup_json)
Handler = site_json
if Mode == 'str':
try:
_info_(View='str', TEXT='%s' % (Handler["name_ripe"]), NickName=Root)
except KeyError:
_info_(View='str', TEXT='Not Found Information', NickName=Root)
try:
_info_(View='str', TEXT='%s' % (Handler["name_rus"]), NickName=Root)
except KeyError:
_info_(View='str', TEXT='Not Found Information', NickName=Root)
except KeyboardInterrupt:
_warning_(View='str', TEXT='KeyboardInterrupt', NickName=Root, TypeMSG='Message')
time.sleep(2)
_error_(View='str', TEXT='KeyboardInterrupt', NickName=Root, Sender='BreakHost', TypeError='WARNING', TypeMSG='Message')
return Sos1skaKeyboardInterrupt('Keyboardinterrupt, pressed "CTRL+C"')
The error was in load_proxy, and I understood why I was passing bytes to it as an argument. Thanks to Michael Butscher
for the note and the question about the load_proxy
argument
Answered By - SOS1SKA EE
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.