Python Socket How To Get Website IP Address – Sometimes, you need to translate a machine’s hostname into its corresponding IP Address, for example, a quick domain name lookup. This article introduces a simple function to do that. If you need to know the IP Address of a Website, you can use a built-in library function, gethostbyname(). In this case, you need to pass the remote hostname as its parameter.
Also you can check Python Socket Programming Articles
1: Python Socket Programming How To Create Socket
2: Python Socket How To Get Local Machine IP Address
So now this is the complete code for this article
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
import socket def main(): remote_host = 'www.codeloop.org' try: print("IP Address Of " + remote_host + " is " + socket.gethostbyname(remote_host)) except socket.error as e: print("Error " , e) if __name__ == "__main__": main() |
How it Works
first of all we need to import socket library , after that we have wraped the gethostbyname() method inside a function called main(). In this article, we introduced the notion of exception handling. As you can see, we wrapped the main function call inside a try-except block. This means that if some error occurs during the execution of this function, this error will be dealt with by this try-except block.
Now run the complete code and this will be the result
Also you can watch the complete video for this article
Subscribe and Get Free Video Courses & Articles in your Email