Python classes in seperate files AttributeError
Python classes in seperate files AttributeError
So, I'm making a vulnerable site checker and I'm doing this using classes. I have one class for fixing the proxies and one for checking. Now I''m done with the proxy class and I started with the check file but I'm getting an error.
proxy.py
class proxy:
def __init__(self):
self.hdr =
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:49.0) Gecko/20100101 Firefox/49.0',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Accept-Language': 'en-US,en;q=0.5',
'Accept-Encoding': 'gzip, deflate',
'DNT': '1',
'Connection': 'keep-alive',
'Upgrade-Insecure-Requests': '1'
def getrandproxy(self):
proxies =
r = requests.get("https://free-proxy-list.net/anonymous-proxy.html", headers=self.hdr)
soup = BS(r.text, "lxml")
container = soup.find("body").section.div.find('div', ['table-responsive']).table.tbody #path from body to proxy table, since parsing directly to it doesn't work
for tr in container.find_all("tr"):
ip = tr.td.text
port = tr.td.next_sibling.text
anonimity = tr.td.next_sibling.next_sibling.next_sibling.next_sibling.text
https = tr.td.next_sibling.next_sibling.next_sibling.next_sibling.next_sibling.next_sibling.text
if https == "yes" and anonimity == "anonymous" or anonimity == "elite proxy":
server = ip + ":" + port
proxies.append(server)
return proxies
injection.py
import requests, proxy
abc = proxy.getrandproxy()
print(abc)
I've tried to print the module on its own, and it worked. I just can't seem to figure out why there's an error. There's some code I cut out from proxy.py because else it would be very long. Tell me if you need all code. By the way the code I cut out isn't necessery, it's just some code with other functions.
error:
Traceback (most recent call last): File "injection.py", line 2, in <module> abc = proxy.getrandproxy()
AttributeError: module 'proxy' has no attribute 'getrandproxy'
It seems like you don't actually intend to write a class. Just remove the
class proxy:
line and all occurrences of self
.– mkrieger1
Aug 20 at 15:34
class proxy:
self
If this class actually has a reason to be a class (that is, its methods have some use for that
self
parameter), then you can’t use it without creating an instance: myproxy = proxy.proxy()
. And then you can call methods on that instance, like myproxy.getrandproxy()
.– abarnert
Aug 20 at 15:48
self
myproxy = proxy.proxy()
myproxy.getrandproxy()
That being said, it’s hard to understand what that class is supposed to represent. It’s called
proxy
, but the only thing it does is hold a header you never use and a method to get a random proxy. Except that method doesn’t even return a random proxy, it returns a list of proxy servers. So, all of the names are misleading, making it very hard to understand what the intended design is, making it even harder to tell you how to fix it to match that design.– abarnert
Aug 20 at 15:50
proxy
@abarnert, the solution you gave me works and thnx for that. Sorry for my messy code but as I said that isn't my only function in my class, and I'm new with using classes so I'm trying to practise. I'm updating my code now, sorry for any confusion.
– user8560436
Aug 20 at 15:59
1 Answer
1
import requests, proxy
abc = proxy.proxy().getrandproxy()
print(abc)
You have a module named proxy
, and inside that module is a class called proxy
proxy
proxy
Or
from proxy import proxy
– Mad Physicist
Aug 20 at 15:28
from proxy import proxy
It gave me this error
Traceback (most recent call last): File "injection.py", line 3, in <module> abc = proxy.getrandproxy() TypeError: getrandproxy() missing 1 required positional argument: 'self'
– user8560436
Aug 20 at 15:31
Traceback (most recent call last): File "injection.py", line 3, in <module> abc = proxy.getrandproxy() TypeError: getrandproxy() missing 1 required positional argument: 'self'
This still doesn’t make any sense, and still won’t work, because
proxy.proxy
is a class, and getrandproxy
is an instance method.– abarnert
Aug 20 at 15:46
proxy.proxy
getrandproxy
thanks, I changed it
– wotanii
Aug 20 at 15:50
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
If you fix the indentation of the first file, you may have an easier time. Otherwise you have a syntax error
– Mad Physicist
Aug 20 at 15:26