Hack Forums
[Python] [Share] Website Resolving [Share] - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [Python] [Share] Website Resolving [Share] (/showthread.php?tid=3396958)



[Share] Website Resolving [Share] - KYZA - Apr 10, 2013

this is really simple Python, but still its good to share code.

This can be very usefull if you like to hit sites ect.

the following code is for Python v2.7.3

Code
import socket
print socket.gethostbyname("google.com")

the output would be googles IP address.

now, with the knowledge of the code above, you can easily upgrade the code.
Here is an example:
Code
import socket, pyperclip, time
    site = raw_input("\n Website: ")
    print("\n {#} Resolving IP Address for %s... {#}" % (site))
    time.sleep(1)
    print("\n {#} IP Address = %s" % socket.gethostbyname(site))
    time.sleep(1)
    print("\n {#} Copying IP Address To Clipboard...")
    pyperclip.copy(socket.gethostbyname(site))
    spam = pyperclip.paste()
    print("\n {#} IP Address Copied To Clipboard!")

so with that script, you can easily type the name of the site & resolve it. Very useful.

There are many things you can do with the module socket, but ill leave that up to you.
I hope you may benefit from this.


* - PigBacon - Apr 10, 2013

Why would you sleep inbetween prints? Seems pointless to mee.
Also, "%s" % whatever is deprecated, look at str.format() instead.


* - KYZA - Apr 10, 2013

(Apr 10, 2013 05:38 AM)plausibility Wrote:  Why would you sleep inbetween prints? Seems pointless to mee.
Also, "%s" % whatever is deprecated, look at str.format() instead.

because I don't like the idea of everything printing at once, and sleep seems like a good solution for that.


* - Skyla Blue - Apr 10, 2013

Pretty basic, but that's ok. It's a good idea to handle exceptions. What if the site doesn't exist? I could type efawegawedgadga.com and chances are it isn't a real site. So put your gethostbyname into a try clause, then for your exception you could say "Not a real website". Not bad though. P.s here is my copy2clipboard function in-case you actually care lol.

Code
def addToClipBoard(text):
    command = 'echo ' + text.strip() + '| clip'
    os.system(command)



* - KYZA - Apr 10, 2013

(Apr 10, 2013 05:41 AM)ExXon^__O Wrote:  Pretty basic, but that's ok. It's a good idea to handle exceptions. What if the site doesn't exist? I could type efawegawedgadga.com and chances are it isn't a real site. So put your gethostbyname into a try clause, then for your exception you could say "Not a real website". Not bad though. P.s here is my copy2clipboard function in-case you actually care lol.

Code
def addToClipBoard(text):
    command = 'echo ' + text.strip() + '| clip'
    os.system(command)

people can do that if they want, I was just sharing something basic and simple, but useful.


* - Skyla Blue - Apr 10, 2013

Yeah they can. I'm just saying, it's good to handle exceptions even in simple things.


* - KYZA - Apr 10, 2013

(Apr 10, 2013 05:46 AM)ExXon^__O Wrote:  Yeah they can. I'm just saying, it's good to handle exceptions even in simple things.

I agree. But if you are using this to hit sites like I said in the thread, you would know if the site you are hitting exists or not, lol. But still I know where you're coming from.


* - K.ing - Apr 10, 2013

ooo. I've never seen that library before pyperclip.

Looks pretty nifty, lets you take the ip adress and puts it in your clipboard.

Does that support win only?


* - KYZA - Apr 13, 2013

(Apr 10, 2013 11:29 AM)K.ing Wrote:  ooo. I've never seen that library before pyperclip.

Looks pretty nifty, lets you take the ip adress and puts it in your clipboard.

Does that support win only?

Sorry for the late reply.

Also, no. It supports just about any OS.