def get_proxy_with_pac(url):
	proxy_str = None
	try:
		pacparser.init()
		pacparser.parse_pac('hk1.pac')
		proxy_str = pacparser.find_proxy(url)
	except:
		sys.stderr.write('could not find proxy for %s using this PAC file.\n' % url)
		return None

	# proxy_str = 'PROXY hkce01.hk.ibm.com:80; PROXY 9.181.193.210:80; DIRECT'
	proxy_list = proxy_str.split(';')
	proxies = {}
	for proxy in proxy_list:
		proxy = proxy.strip()
		if 'DIRECT' == proxy:
			continue
		if proxy[0:5].upper() == 'PROXY':
			proxy = proxy[6:].strip()
			if is_proxy_alive(proxy):
				proxies['http'] = proxy
				break
	sys.stdout.write('get proxy %s for %s\n' % (proxies, url)) 

	return proxies
Beispiel #2
0
def test_pac(pacfile, testfile):
    """Test PAC file against test data using pacparser

    Returns 0 if all tests pass, 1 if any fail"""

    # Stores the script's exit code
    testfailed = 0

    v_print(3, "Testing PAC file: {}".format(pacfile))

    # Initialise pacparser
    pacparser.init()
    pacparser.parse_pac(pacfile)

    with open(testfile, 'rt') as f:
        # Create csv reader, filtering out rows starting with '#'
        reader = csv.DictReader(filter(lambda row: row[0] != '#', f),
                                delimiter=',')

        # Iterate over test data
        for row in reader:
            v_print(1, "\nread row: {} {}".format(row['url'], row['expected']))
            testfailed = test_url(row['url'], row['expected'])

    # Cleanup pacparser
    pacparser.cleanup()

    if (testfailed):
        print("PAC file Test Failed", file=sys.stderr)
    else:
        v_print(3, "PAC file Test Passed")

    return testfailed
def find( site, pacfile, myip = "" ):
  pacparser.init()
  pac_file_cache = cache_pacfile.cache(pacfile)
  if not myip == "":
    pacparser.setmyip(myip)
  pacparser.parse_pac(pac_file_cache)

  return pacparser.find_proxy(site) 
Beispiel #4
0
def main_test(filename, test_times):
	pacparser.init()
	pacparser.parse_pac(filename)
	beg_time = time.time()
	for i in xrange(test_times):
		ret_str = pacparser.find_proxy('http://www.coding.com', 'www.coding.com') # using the worst case
	end_time = time.time()
	print "%s:\nTotal Time: %s s\nAvg. Time: %s ms\n\n" % (filename, end_time - beg_time, (end_time - beg_time) * 1000.0 / test_times),
	pacparser.cleanup()
Beispiel #5
0
def index(pacfile=None):

    myip = request.args.get('myip', '')
    url = request.args.get('url', 'http://www.google.com')

    pp.init()
    pp.setmyip(myip)
    pp.parse_pac('test.pac')
    proxy = pp.find_proxy(url)

    return proxy
Beispiel #6
0
def main_test(filename, test_times):
    pacparser.init()
    pacparser.parse_pac(filename)
    beg_time = time.time()
    for i in xrange(test_times):
        ret_str = pacparser.find_proxy(
            'http://www.coding.com', 'www.coding.com')  # using the worst case
    end_time = time.time()
    print "%s:\nTotal Time: %s s\nAvg. Time: %s ms\n\n" % (
        filename, end_time - beg_time,
        (end_time - beg_time) * 1000.0 / test_times),
    pacparser.cleanup()
Beispiel #7
0
def get_pac_result(filename, url, host):
	pacparser.init()
	pacparser.parse_pac(filename)
	ret_str = pacparser.find_proxy(url, host)
	pacparser.cleanup()
	return ret_str
Beispiel #8
0
#!/usr/bin/env python

import pacparser,sys

print sys.argv[1]

if len(sys.argv) >= 3:
    pacfile=sys.argv[2]
    print "use: ", pacfile
else:
    pacfile="proxy.pac"

pacparser.init()
pacparser.parse_pac(pacfile)
proxy = pacparser.find_proxy(sys.argv[1])
print proxy
pacparser.cleanup()

# Or simply,
print pacparser.just_find_proxy(pacfile, sys.argv[1])
Beispiel #9
0
def get_pac_result(filename, url, host):
    pacparser.init()
    pacparser.parse_pac(filename)
    ret_str = pacparser.find_proxy(url, host)
    pacparser.cleanup()
    return ret_str
Beispiel #10
0
#!/usr/bin/python2.5

import pacparser

pacparser.init()
pacparser.parse_pac("wpad.dat")
proxy = pacparser.find_proxy("http://www.manugarg.com")
print proxy
pacparser.cleanup()

# Or simply,
print pacparser.just_find_proxy("wpad.dat", "http://www2.manugarg.com")
Beispiel #11
0
    return proxy_exception_final


def run_command(command):
    #print "bash: %s" % command
    p = subprocess.Popen(command,
                         stdout=subprocess.PIPE,
                         shell=isinstance(command, str))
    return iter(p.stdout.readline, b'')


if __name__ == '__main__':
    # get current proxy server
    pacparser.init()

    pacparser.parse_pac(pac_file)
    output = pacparser.find_proxy('http://captive.apple.com',
                                  'captive.apple.com')
    proxies = output.split("; ")
    proxies = [p.replace('PROXY ', '') for p in proxies]
    (main_proxy_url, main_proxy_port) = proxies[0].split(":")
    print "### Proxy set to: %s, port %s" % (main_proxy_url, main_proxy_port)

    # Create a new network location so we don't break existing settings
    if location_name in run_command(list_locations_cmd):
        run_command(delete_location_cmd)
    run_command(create_location_cmd)
    run_command(switch_location_cmd)

    # Get list of all the interfaces
    list_network_services = run_command(list_interfaces_cmd)