Jan-05-2017, 04:13 AM
my latest edit:
#!/usr/bin/env python3 # -*- coding: utf-8 -*- from __future__ import division, print_function, unicode_literals """ file getmyip.py purpose Get the public IP address of this host command python getmyip.py function get_this_ip() aruments none note this script accesses multiple web sites in parallel by means of the multiprocessing module. email 10054452614123394844460370234029112340408691 The intent is that this command works correctly under both Python 2 and Python 3. Please report failures or code improvement to the author. """ __license__ = """ Copyright (C) 2017, by Phil D. Howard - all other rights reserved Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA, OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE, OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. The author may be contacted by decoding the number 10054452614123394844460370234029112340408691 (provu igi la numeron al duuma) """ import os, sys env = os.environ from collections import Counter from multiprocessing import Pipe, Process from sys import argv, exc_info, stderr, stdout, version_info from time import sleep if version_info.major < 3: from urllib2 import URLError, urlopen else: from urllib.request import urlopen from urllib.error import URLError http_timeout = 30 site_list = [ ( 'http://icanhazip.com/', None ), ( 'http://ipecho.net/plain', None ), ( 'http://ipinfo.io/ip', None ), ( 'http://whatismyip.akamai.com/', None ), ( 'http://v4.ipv6-test.com/api/myip.php', None ), ( 'https://api.ipify.org?format=json', 'json', 'ip' ), ( 'http://jsonip.com/', 'json', 'ip' ), ( 'http://ipinfo.io/json', 'json', 'ip' ), ( 'https://ifconfig.co/json', 'json', 'ip' ), ( 'http://ip-api.com/json', 'json', 'query' ), ] def child( site_index, this_pipe ): """ function child purpose run in each of many child processes to get the IP address as this IP address server site sees it. argument 1 (int) index (starting at 0) of which site to access. 2 (int) fd of pipe to send result back to parent outputs IP address returns (int) return status code """ if site_index >= len( site_list ): return -1 url, method, extra = (site_list[site_index]+(0,))[:3] os.close(this_pipe[0]) os.dup2(this_pipe[1],1) os.close(this_pipe[1]) try: conn = urlopen( url, timeout = http_timeout ) data = conn.read().strip() except URLError: data = None if version_info.major > 2: data = data.decode() addr = data = data.strip() if method == 'json': import json addr = json.loads(data)[extra] if method == 'html': data = ' '.join('>'.join(data.split('<')).split('>')).split() addr = data[extra] if 'verbose' in env: print(addr,'from',url,file=stderr) if addr in (None,False,True): os._exit(1) if len(addr) > 0: print( addr ) exit(0) def get_many_ips( number_of_sites ): """ function get_many_ips purpose run many child processes, with a pipe from each, to get the IP address this host appears to have as seen by each of a list of IP address sources. arguments -none- returns (list) of IP addresses """ pipes = [] procs = [] addrs = [] sleepy = 3.0 / 256.0 site_range = range( number_of_sites ) for site_index in site_range: this_pipe = os.pipe() this_proc = Process( target = child, args = (site_index,this_pipe) ) pipes.append( this_pipe ) procs.append( this_proc ) this_proc.start() os.close(this_pipe[1]) for site_index in site_range: addr = '' if version_info.major > 2: addr = addr.encode() while True: data = os.read(pipes[site_index][0],4096) if len(data) > 0: addr += data if len(data) < 1: break if version_info.major > 2: addr = addr.decode() addrs.append( addr.strip() ) sleep( sleepy ) for site_index in site_range: procs[site_index].join( 360 ) return addrs def get_this_ip(): """ function get_my_ip purpose gets a list of IP addresses from get_many_ips() and one most common IP address from that list. arguments -none- returns (str) most common IP address, (int) agree count, (int) total sources count """ tutaj = Counter() addrs = get_many_ips( len( site_list ) ) for addr in addrs: tutaj[addr] += 1 addr = tutaj.most_common(1)[0][0] agree = tutaj.most_common(1)[0][1] sources = len(addrs) return (addr,agree,sources) def get_ip(): return get_this_ip()[0] def main( args ): if version_info.major > 2: typestr = str else: typestr = basestring addr,agree,sources = get_this_ip() if addr == None: print( 'failed to get IP address, got:', repr(addr), file=stderr ) return 1 if not isinstance( addr, typestr ): print( 'failed to get an IP address, got:', repr(addr), file=stderr ) return 2 if len( addr ) < 9: print( 'failed to get a valid IP address, got:', repr(addr), file=stderr ) return 3 if '.' not in addr: print( 'failed to get any valid IP address, got:', repr(addr), file=stderr ) return 4 print( '{} of {} sources agree that your IP address is:'.format(repr(agree),repr(sources)), file=stderr ) stderr.flush() print( addr ) return 0 if __name__ == '__main__': try: result = main( argv ) stdout.flush() except KeyboardInterrupt: result = 141 print( '' ) except IOError: result = 142 try: exit( int( result ) ) except ValueError: print( str( result ), file=stderr ) exit( 1 ) except TypeError: if result == None: exit( 0 ) exit( 255 ) # EOFnow it runs on python3. the child function was changed from doing os.write() to doing print() over the pipe back to the parent.
Tradition is peer pressure from dead people
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.