Skip to content

Instantly share code, notes, and snippets.

@vozlt
Last active February 16, 2016 09:38
Show Gist options
  • Save vozlt/e05cc8c6247fb48054dd to your computer and use it in GitHub Desktop.
Save vozlt/e05cc8c6247fb48054dd to your computer and use it in GitHub Desktop.
Check my ip address
#!/usr/bin/python
#
# -*- coding: utf-8 -*-
#
# @file: checkmyip.py
# @brief: Python program for ip check with tor + privoxy + squid
# @author: YoungJoo.Kim <vozlt@vozlt.com>
# @version:
# @date: 20131030
import sys
import getopt
import httplib
import urllib2
import urlparse
_program_name = 'checkmyip'
_default_cfgs = {
'url': 'http://ip.vozlt.com/myip/text',
'proxy_host': 'localhost:3128',
}
def main():
try:
opts, args = getopt.getopt(sys.argv[1:], "hu:p:", ["help", "url=", "proxy="])
except getopt.GetoptError:
usage()
url = None
proxy_host = None
for option, value in opts:
if option in ("-h", "--help"):
usage()
elif option in ("-u", "--url"):
url = value
elif option in ("-p", "--proxy"):
proxy_host = value
else:
usage()
info = CheckInfo(url, proxy_host)
info.getUrl()
def usage():
print "Usage: %s [-u URL] [-p PROXY:PORT]" % _program_name
print
print "Options:"
print " -u, --url=[URL] HTTP URL [Default: %s]" % _default_cfgs['url']
print " -p, --proxy=[SERVER:PORT] Proxy server"
print " -h, --help Help"
print " "
print "Examples:"
print " %s" % _program_name
print " %s -u %s" % (_program_name, _default_cfgs['url'])
print " %s -p %s" % (_program_name, _default_cfgs['proxy_host'])
print " %s -u %s -p %s" % (_program_name, _default_cfgs['url'], _default_cfgs['proxy_host'])
sys.exit(2);
class CheckInfo:
def __init__(self, url=None, proxy_host=None):
if url is None:
url = _default_cfgs['url']
self.cfgs = {}
self.cfgs['url'] = url
self.cfgs['proxy_host'] = proxy_host
self.urls = urlparse.urlparse(url)
def getUrl(self):
if self.cfgs['proxy_host'] is None:
self.getHttp()
else:
self.getProxy()
def getUri(self):
urls = self.urls
uri = urls.path
if urls.path is None:
uri = '/'
if urls.query:
uri = urls.path + '?' + urls.query
return uri
def getHttp(self):
conn = None
if self.urls.scheme == "https":
conn = httplib.HTTPSConnection(self.urls.netloc)
elif self.urls.scheme == "http":
conn = httplib.HTTPConnection(self.urls.netloc)
conn.request("GET", self.getUri())
response = conn.getresponse()
data = response.read()
conn.close()
print ">>> %s\n" % (self.cfgs['url'])
print data
def getProxy(self):
proxies = {"http":"http://%s" % self.cfgs['proxy_host'] }
url = self.cfgs['url']
headers = {'User-agent':'Mozilla/5.0'}
proxy_support = urllib2.ProxyHandler(proxies)
opener = urllib2.build_opener(proxy_support)
urllib2.install_opener(opener)
request = urllib2.Request(url, None, headers)
response = None
try:
response = urllib2.urlopen(request)
except urllib2.HTTPError, err:
print "[ERROR] : " + str(err.code)
print
print "Details:"
print " URL: %s" % url
print " PROXY: %s" % self.cfgs['proxy_host']
print " RESPONSE CODE: %s" % str(err.code)
sys.exit(1)
data = response.read()
print ">>> %s -> %s\n" % (self.cfgs['url'], self.cfgs['proxy_host'])
print data
if __name__ == "__main__": main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment