def test_urllib2_http(self): original_socket = urllib2.socket.socket try: self.test_server.response['data'] = b'zzz' socks.set_default_proxy(socks.HTTP, PROXY_HOST_IP, HTTP_PROXY_PORT) socks.wrap_module(urllib2) address = (TEST_SERVER_HOST, TEST_SERVER_PORT) url = 'http://%s:%d/' % address res = urllib2.urlopen(url) resp_body = res.read() self.assertEqual(200, res.getcode()) self.assertEqual(b'zzz', resp_body) self.assertTrue(self.test_server.request['headers'] ['user-agent'].startswith('Python-urllib')) self.assertEqual('%s:%d' % address, self.test_server.request['headers']['host']) finally: urllib2.socket.socket = original_socket
def test_urllib2_socks5(self): wait_event, server_queue = start_extra_test_server() original_socket = urllib2.socket.socket try: socks.set_default_proxy(socks.SOCKS5, config.PROXY_HOST_IP, config.SOCKS5_PROXY_PORT) socks.wrap_module(urllib2) address = (config.TEST_SERVER_HOST, config.TEST_SERVER_EXTRA_PORT) url = 'http://%s:%d/' % address res = urllib2.urlopen(url) resp_body = res.read() wait_event.set() request = server_queue.get(block=True, timeout=1) self.assertEqual(200, res.getcode()) self.assertEqual(b'zzz', resp_body) self.assertTrue( request['headers']['user-agent'].startswith('Python-urllib')) self.assertEqual('%s:%d' % address, request['headers']['host']) finally: urllib2.socket.socket = original_socket
def create_service(ip_addr, port_no, client_secret_file, api_name, api_version, *scopes): IP_ADDR = ip_addr PORT_NO = port_no if IP_ADDR is not None: socks.set_default_proxy(socks.PROXY_TYPE_HTTP, IP_ADDR, PORT_NO) socks.wrap_module(httplib2) # If modifying these scopes, delete the file token.pickle. SCOPES = [scope for scope in scopes[0]] CLIENT_SECRET_FILE = client_secret_file API_NAME = api_name API_VERSION = api_version creds = None # The file token.pickle stores the user's access and refresh tokens, and is # created automatically when the authorization flow completes for the first # time. if os.path.exists('token.pickle'): with open('token.pickle', 'rb') as token: creds = pickle.load(token) # If there are no (valid) credentials available, let the user log in. if not creds or not creds.valid: if creds and creds.expired and creds.refresh_token: creds.refresh(Request()) else: flow = InstalledAppFlow.from_client_secrets_file( CLIENT_SECRET_FILE, SCOPES) creds = flow.run_local_server(port=0) # Save the credentials for the next run with open('token.pickle', 'wb') as token: pickle.dump(creds, token) try: service = build(API_NAME, API_VERSION, credentials=creds) print(API_NAME, "service created successfully.") return service except Exception as e: print("Unable to connect.") print(e) return None
def check_one_proxy(checkmothed, ip, port, method): global update_array global check_in_one_call global target_url, target_string, target_timeout url = target_url checkstr = target_string timeout = target_timeout if checkmothed == 'http': if method == 1: proxy_handler = urllib2.ProxyHandler( {'http': 'http://' + ip + ':' + str(port) + '/'}) opener = urllib2.build_opener(proxy_handler) urllib2.install_opener(opener) else: return # socks4,socks5 退出函数处理 elif checkmothed == 'connect': if method == 1: socks.setdefaultproxy(socks.PROXY_TYPE_HTTP, ip, int(port)) elif method == 2: socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS4, ip, int(port)) elif method == 3: socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5, ip, int(port)) socks.wrap_module(urllib2) send_headers = { 'User-agent': 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)' } t1 = time.time() try: req = urllib2.Request(url, headers=send_headers) r = urllib2.urlopen(req, timeout=20) rehtml = r.read() pos = rehtml.find(checkstr) except Exception, e: pos = -1 print e
def check_one_proxy(checkmothed,ip,port,method): global update_array global check_in_one_call global target_url,target_string,target_timeout url=target_url checkstr=target_string timeout=target_timeout if checkmothed=='http': if method==1: proxy_handler = urllib2.ProxyHandler({'http': 'http://'+ip+':'+str(port)+'/'}) opener = urllib2.build_opener(proxy_handler) urllib2.install_opener(opener) else: return # socks4,socks5 退出函数处理 elif checkmothed=='connect': if method==1: socks.setdefaultproxy(socks.PROXY_TYPE_HTTP, ip, int(port)) elif method==2: socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS4, ip, int(port)) elif method==3: socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5, ip, int(port)) socks.wrap_module(urllib2) send_headers = { 'User-agent':'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)' } t1=time.time() try: req = urllib2.Request(url,headers=send_headers) r = urllib2.urlopen(req,timeout=20) rehtml=r.read() pos=rehtml.find(checkstr) except Exception,e: pos=-1 print e
def urllib2_SOCKS5_test(): socks.set_default_proxy(socks.SOCKS5, "127.0.0.1", 1081) socks.wrap_module(urllib2) status = urllib2.urlopen("http://api.externalip.net/ip/").getcode() assert status == 200
if args.proxy_file: raise NotImplementedError('Coming soon.') if args.proxy: def create_connection(address, timeout=None, source_address=None): sock = socks.socksocket() sock.connect(address) return sock proxy_host, proxy_port = args.proxy.split(':') # Patch the socket module socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5, proxy_host, int(proxy_port), rdns=True) # rdns is by default on true. Never use rnds=False with TOR, otherwise you are screwed! socks.wrap_module(socket) socket.create_connection = create_connection valid_search_types = ('normal', 'video', 'news', 'image') if args.searchtype not in valid_search_types: ValueError('Invalid search type! Select one of {}'.format(repr(valid_search_types))) if args.deep_scrape: results = deep_scrape(args.query) else: results = scrape(args.query, args.num_results_per_page, args.num_pages, searchtype=args.searchtype) for result in results: logger.info('{} links found! The search with the keyword "{}" yielded the result:{}'.format( len(result['results']), result['search_keyword'], result['num_results_for_kw'])) if args.view:
def __init__(self): # get info from the config sc = SC() self.me = sc.smtp_from self.username = sc.smtp_username self.password = sc.smtp_password self.server_name = sc.smtp_server self.server_port = sc.smtp_port self.log = '' if sc.use_proxy is True: # try to wrap the smtplib library with the socks module if sc.proxy_username and sc.proxy_password: try: socks.set_default_proxy('socks.PROXY_TYPE_SOCKS4', sc.proxy_server, sc.proxy_port, username=sc.proxy_username, password=sc.proxy_password) socks.wrap_module(smtplib) except: try: socks.set_default_proxy('socks.PROXY_TYPE_SOCKS5', sc.proxy_server, sc.proxy_port, username=sc.proxy_username, password=sc.proxy_password) socks.wrap_module(smtplib) except: try: socks.set_default_proxy('socks.PROXY_TYPE_SOCKS4', sc.proxy_server, sc.proxy_port) socks.wrap_module(smtplib) except: try: socks.set_default_proxy( 'socks.PROXY_TYPE_SOCKS5', sc.proxy_server, sc.proxy_port) socks.wrap_module(smtplib) except: self.log += 'Unable to access SMTP through proxy' else: try: socks.set_default_proxy('socks.PROXY_TYPE_SOCKS4', sc.proxy_server, sc.proxy_port) socks.wrap_module(smtplib) except: try: socks.set_default_proxy('socks.PROXY_TYPE_SOCKS5', sc.proxy_server, sc.proxy_port) socks.wrap_module(smtplib) except: self.log += 'Unable to access SMTP through proxy'
def lookup_mx_record(domain): try: records = dns.resolver.query(domain, 'MX') except: return False mx_record = records[0].exchange return str(mx_record) mx_record_cache = {} validate_result = [] emails_to_verify = read_emails_by_domain() print "Resolving mx domain..." for i, j in emails_to_verify.iteritems(): mx_record_cache[i] = lookup_mx_record(i) proxy_host = "127.0.0.1" proxy_port = 9050 socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5, proxy_host, proxy_port) socks.wrap_module(smtplib) threads = execute_thread(emails_to_verify) for t in threads: t.join(20) out_file = open("result", "wb") for i in validate_result: out_file.write(i + "\n")
def urllib2_HTTP_test(): socks.set_default_proxy(socks.HTTP, "127.0.0.1", 8081) socks.wrap_module(urllib2) status = urllib2.urlopen("http://ifconfig.me/ip").getcode() assert status == 200
from oauth2client import client from oauth2client import tools from michel.tasktree import TaskLink, TasksTree, OrgDate from michel import utils if 'HTTP_PROXY' in os.environ: try: import socks http_proxy = re.match("^(?P<scheme>http|https|socks):\/\/(?:(?P<username>[^:]+):(?P<password>[^@]+)@)?(?P<address>[^:]+)(?::(?P<port>\d+))?$", os.environ['HTTP_PROXY']) socks.set_default_proxy(socks.HTTP, http_proxy.group('address'), int(http_proxy.group('port')), username=http_proxy.group('username'), password=http_proxy.group('password')) socks.wrap_module(httplib2) except: print("HTTP Proxy cannot be used, please install pysocks", file=sys.stderr) sys.exit(1) class GtaskProvider: _sys_regex = re.compile(":PARENT: (.*)") _google_time_regex = re.compile("(\d+)-(\d+)-(\d+)T(\d+):(\d+):(\d+).+") def __init__(self, path, params): self._profile_name = path[0] self._list_name = path[1] self._tasks_tree = None self._task_id_map = None
raise NotImplementedError('Coming soon.') if args.proxy: def create_connection(address, timeout=None, source_address=None): sock = socks.socksocket() sock.connect(address) return sock proxy_host, proxy_port = args.proxy.split(':') # Patch the socket module socks.setdefaultproxy( socks.PROXY_TYPE_SOCKS5, proxy_host, int(proxy_port), rdns=True ) # rdns is by default on true. Never use rnds=False with TOR, otherwise you are screwed! socks.wrap_module(socket) socket.create_connection = create_connection if args.deep_scrape: results = deep_scrape(args.query) else: results = scrape(args.query, args.num_results_per_page, args.num_pages) for result in results: logger.info( '{} links found! The search with the keyword "{}" yielded the result:{}' .format(len(result['results']), result['search_keyword'], result['num_results_for_kw'])) if args.view: import webbrowser webbrowser.open(result['cache_file'])