Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

Python 

Error Handling? Function throws errors?

Code
domains = ['http://www.sdfjkshdlfkghsdklfhg.com','google.com',etc...]

try:
    test = whois(domains[0])
    print(type(test.status))
except ValueError:
    print("Invalud Domain")

So, I am seeing if a site exists using whois but if the domain DOESN'T exist it breaks the whole thing and stops.

How can I just print out "Invalid" and move onto the next item in the list.
The way I typically sanitize user input is by a safe cast function. You can except ValueError since that is what would be raise, but in this case, there's probably no reason to do that.

PHP Code

<?php 
def safe_cast
(new_type, value, default=None):
    try:
        default = 
new_type(value)
    finally:
        return default 

There's also a pretty wide use for a temporary storage class that can be used to store and then check user input without having a ton of statements to get around the fact that there's no do/while loop. For example, if you wanted to force the user to keep entering user input until they entered something valid, you can do it with a simple class:

PHP Code

<?php 
class Storage:
    
def __init__(self):
        
self.val = None

    def set
(self, val):
        
self.val = val
        
return val

    def get
(self):
        return 
self.val 

Now, to accept user input and such...

PHP Code

<?php 
def is_valid_domain
(domain):
    if 
domain == "google.com":  # arbitrary checking here... provide whatever checks you want
        
return True
    
return False

storage 
= Storage()  # an instance of the storage class

while is_valid_domain(storage.set(input("Enter a domain: "))) is False:
    print(
"Invalid domain. ", end="")  # python3 only...

domain = storage.get()  # you can guarantee that the value passed the is_valid_domain check

print("Domain is {}".format(domain)) 
SRPP Discord Server

I am a professional network/cloud security architect.
My Old Tutorials
Ah, my apologies. Well, I'm not familiar with the whois module specifically, but I assume you're more than capable of determining what type of error it throws...

If that's the case, then here's something along the lines of what I'd do:

PHP Code

<?php 
my_list_of_domains 
= ["domain.com", "example.com", ]  # etc...

for domain in my_list_of_domains:
    try:
        
whois.check_valid_domain(domain)  # function that throws an error if the domain is invalid
        
continue
    
except WhoisException as e:  # assuming the exception thrown is a WhoisException
        # maybe you want to check the error, e, to see what subtype it is, I'm not sure
        
with open("available_domains.txt", "a+") as fout:
            
fout.write("{}\n".format(domain)) 
SRPP Discord Server

I am a professional network/cloud security architect.
My Old Tutorials


Possibly Related Threads...
Thread / Author Replies Views Last Post
[C] by FhackMar 2, 2022 03:52 AM 4 307 Mar 2, 2022 03:52 AM
Last Post: Fhack
[C#] by KUi29929Aug 11, 2020 12:09 PM 1 296 Aug 11, 2020 12:09 PM
Last Post: VisualSoft
[Python] by h0lyl337Dec 11, 2019 09:35 AM 2 132 Dec 11, 2019 09:35 AM
Last Post: h0lyl337