Last active
April 22, 2020 06:40
-
-
Save erm3nda/b8f0db13343780fe4db4240d29acca01 to your computer and use it in GitHub Desktop.
6 ways to get the current ip using Python and it's modules
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
#! -*- coding: utf-8 -*- | |
# TODO: fix ip_firefox() and ip_socket() functions | |
# NOTE: Note that PhantomJS is blazing fast to being a fully javascript capable browser who makes the connection. | |
print "" | |
import sys, json, time, selenium, urllib, urllib2, urllib3, requests, socket | |
from selenium import webdriver | |
def save_file(savefile, content): | |
with open(savefile + "_output.txt", "w+") as file: | |
file.write(content) | |
def ip_urllib(): | |
start_time = time.time() | |
print sys._getframe().f_code.co_name, | |
content = urllib.urlopen("http://httpbin.org/ip").read() | |
save_file(sys._getframe().f_code.co_name, content) | |
elapsed = time.time() - start_time | |
print elapsed, "seconds", | |
print json.loads(content)["origin"] | |
return True | |
def ip_urllib2(): | |
start_time = time.time() | |
print sys._getframe().f_code.co_name, | |
content = urllib2.urlopen("http://httpbin.org/ip").read() | |
save_file(sys._getframe().f_code.co_name, content) | |
elapsed = time.time() - start_time | |
print elapsed, "seconds", | |
print json.loads(content)["origin"] | |
return True | |
def ip_urllib3(): | |
start_time = time.time() | |
print sys._getframe().f_code.co_name, | |
http = urllib3.PoolManager() | |
content = http.request('GET', 'http://httpbin.org/ip').data | |
elapsed = time.time() - start_time | |
print elapsed, "seconds", | |
print json.loads(content)["origin"] | |
return True | |
def ip_requests(): | |
start_time = time.time() | |
print sys._getframe().f_code.co_name, | |
content = requests.get("http://httpbin.org/ip").text | |
elapsed = time.time() - start_time | |
print elapsed, "seconds", | |
print json.loads(content)["origin"] | |
return True | |
# I never use sockets, best urllib i think it is 2 or even 3 | |
def ip_socket(): | |
start_time = time.time() | |
print sys._getframe().f_code.co_name, | |
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
s.connect_ex(("httpbin.org", 80)) | |
s.send("GET /ip HTTP/1.0 \n") | |
content = s.recv(1024) | |
elapsed = time.time() - start_time | |
s.close | |
print elapsed, "seconds", | |
print json.loads(content)["origin"], | |
return True | |
# case you need to use Javascript enabled browser connection ;) | |
def ip_phantomjs(): | |
start_time = time.time() | |
print sys._getframe().f_code.co_name, | |
driver = webdriver.PhantomJS() | |
driver.get("http://httpbin.org/ip") | |
#driver.find_element_by_partial_link_text("ip").click() # ip json_encoded data | |
save_file(sys._getframe().f_code.co_name, json.loads(driver.find_element_by_tag_name("body").text)["origin"]) | |
elapsed = time.time() - start_time | |
print elapsed, "seconds", | |
print json.loads(driver.find_element_by_tag_name("body").text)["origin"], # that should output just a ip :C | |
return True | |
def ip_firefox(): | |
start_time = time.time() | |
print sys._getframe().f_code.co_name, | |
fp = webdriver.FirefoxProfile() | |
driver = webdriver.Firefox(firefox_profile=fp) | |
driver.get("http://httpbin.org/ip") | |
#driver.find_element_by_partial_link_text("ip").click() # ip json_encoded data | |
save_file(sys._getframe().f_code.co_name, json.loads(driver.find_element_by_tag_name("body").text)["origin"]) | |
elapsed = time.time() - start_time | |
driver.quit() | |
print elapsed, "seconds", | |
print json.loads(driver.find_element_by_tag_name("body").text)["origin"] # that should output just a ip :C | |
return True | |
if __name__ == "__main__": | |
ip_urllib() | |
ip_urllib2() | |
ip_urllib3() | |
ip_requests() | |
#ip_socket() # wtf? | |
ip_phantomjs() # it's blazing fast :V | |
#ip_firefox() # wtf? |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment