我们从Python开源项目中,提取了以下21个代码示例,用于说明如何使用socks.setdefaultproxy()。
def __init__(self, roomId): proxyIp = "216.189.158.147" proxyPort = 52940 # socks ?????? # self.sock = socks.socksocket() # self.sock.set_proxy(socks.SOCKS5, proxyIp, proxyPort) self.mongo_clent = MongoDBClient.MongoDBClient('douyu') # socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5, proxyIp, proxyPort) socks.set_default_proxy(socks.PROXY_TYPE_SOCKS5, proxyIp, proxyPort) socket.socket = socks.socksocket self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.codeLocalToServer = 689 self.serverToLocal = 690 self.gid = -9999 self.roomId = roomId self.server = {} self.log("??? :" + str(self.roomId))
def run(self): # la funzione che da' le istruzioni ai vari threads data = random._urandom(1024) # data per il pacchetto random p = bytes(IP(dst=str(url2))/TCP(sport=RandShort(), dport=int(port))/data) # costruzione pacchetto tcp + data current = x # per dare l'id al thread if current < len(proxies): # se l'id del thread si puo' associare ad un proxy, usa quel proxy proxy = proxies[current].strip().split(':') else: # altrimenti lo prende a random proxy = random.choice(proxies).strip().split(":") go.wait() # aspetta che tutti i proxy siano pronti while True: try: socks.setdefaultproxy(socks.PROXY_TYPE_HTTP, str(proxy[0]), int(proxy[1]), True) # comando per il proxying HTTP s = socks.socksocket() # creazione socket s.connect((str(url2),int(port))) # si connette s.send(p) # ed invia print ("Request sent from " + str(proxy[0]+":"+proxy[1]) + " @", self.counter) # print req + counter except: # se si verifica un errore s.close() # chiude il thread e ricomincia
def run(self): # la funzione che da' le istruzioni ai vari threads data = random._urandom(1024) # data per il pacchetto random p = bytes(IP(dst=str(url2))/UDP(dport=int(port))/data) # crea pacchetto udp classico + data current = x # per dare l'id al thread if current < len(proxies): # se l'id del thread si puo' associare ad un proxy, usa quel proxy proxy = proxies[current].strip().split(':') else: # altrimenti lo prende a random proxy = random.choice(proxies).strip().split(":") go.wait() # aspetta che threads sono pronti while True: try: socks.setdefaultproxy(socks.PROXY_TYPE_HTTP, str(proxy[0]), int(proxy[1]), True) # comando per il proxying HTTP s = socks.socksocket() # creazione socket s.connect((str(url2),int(port))) # connessione s.send(p) # invio print ("Request sent from " + str(proxy[0]+":"+proxy[1]) + " @", self.counter) # print req + counter except: # se qualcosa va storto s.close() # chiude il socket
def URL(url): socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5, "127.0.0.1", 9050) socket.socket = socks.socksocket socket.create_connection = create_connection file_name = url.split('/')[-1] u = urllib2.urlopen(url) f = open(file_name, 'wb') meta = u.info() file_size = int(meta.getheaders("Content-Length")[0]) print "Downloading: %s Bytes: %s" % (file_name, file_size) file_size_dl = 0 block_sz = 8192 while True: buffer = u.read(block_sz) if not buffer: break file_size_dl += len(buffer) f.write(buffer) status = r"%10d [%3.2f%%]" % (file_size_dl, file_size_dl * 100. / file_size) status = status + chr(8)*(len(status)+1) print status, f.close() print ""
def connectTor(): try: SOCKS_PORT = 9050 # Set socks proxy and wrap the urllib module socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5, '127.0.0.1', SOCKS_PORT) socket.socket = socks.socksocket # Perform DNS resolution through the socket def getaddrinfo(*args): return [(socket.AF_INET, socket.SOCK_STREAM, 6, '', (args[0], args[1]))] socket.getaddrinfo = getaddrinfo except: e = sys.exc_info()[0] print( "Error: %s" % e +"\n## Can't establish connection with TOR")
def __init__(self, room_id): threading.Thread.__init__(self) proxyIp = "116.255.153.137" proxyPort = 8082 socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5, proxyIp, proxyPort) # socket.socket = socks.socksocket self.mongo_clent = MongoDBClient.MongoDBClient('douyu') self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.codeLocalToServer = 689 self.serverToLocal = 690 self.gid = -9999 self.roomId = room_id self.server = {} self.log("??? :" + str(self.roomId))
def __init__(self): proxyIp = "116.255.153.137" proxyPort = 8082 socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5, proxyIp, proxyPort) # socket.socket = socks.socksocket self.mongo_clent = MongoDBClient.MongoDBClient('douyu') self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.codeLocalToServer = 689 self.serverToLocal = 690 self.gid = -9999 self.rid = 16789 self.server = {}
def _connect(self): global _server_ip, _proxy_ip host = _server_ip.get_server_ip(self.whois_srv) # ????? host = host if host else self.whois_srv if flag_proxy: proxy_info = _proxy_ip.get(self.whois_srv) # ??IP if proxy_info is not None: socks.setdefaultproxy( proxytype=socks.PROXY_TYPE_SOCKS4 if proxy_info.mode == 4 else socks.PROXY_TYPE_SOCKS5, addr=proxy_info.ip, port=proxy_info.port) socket.socket = socks.socksocket self.tcpCliSock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) socket.setdefaulttimeout(20) data_result = "" try: self.tcpCliSock.connect((host, 43)) self.tcpCliSock.send(self.request_data + '\r\n') except socket.error as e: if str(e).find("timed out") != -1: # ???? return "ERROR -1" elif str(e).find("Temporary failure in name resolution") != -1: return "ERROR -2" else: return "ERROR OTHER" while True: try: data_rcv = self.tcpCliSock.recv(1024) except socket.error as e: return "ERROR -3" if not len(data_rcv): return data_result # ?????? data_result = data_result + data_rcv # ????????
def run(self): # la funzione che da' le istruzioni ai vari threads data = random._urandom(1024) # data per il pacchetto random p = bytes(IP(dst=str(url2))/TCP(sport=RandShort(), dport=int(port))/data) # costruzione pacchetto tcp + data current = x # per dare l'id al thread if current < len(proxies): # se l'id del thread si puo' associare ad un proxy, usa quel proxy proxy = proxies[current].strip().split(':') else: # altrimenti lo prende a random proxy = random.choice(proxies).strip().split(":") go.wait() # aspetta che threads siano pronti while True: try: socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5, str(proxy[0]), int(proxy[1]), True) # comando per il proxying via SOCKS s = socks.socksocket() # creazione socket s.connect((str(url2),int(port))) # si connette s.send(p) # ed invia print ("Request sent from " + str(proxy[0]+":"+proxy[1]) + " @", self.counter) # print req + counter except: # se si verifica un errore s.close() # intanto chiude il precedente socket non funzionante try: socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS4, str(proxy[0]), int(proxy[1]), True) # poi prova ad utilizzare SOCKS4, magari e' questo il problema dell'errore s = socks.socksocket() # creazione socket s.connect((str(url2),int(port))) # connessione s.send(p) # invio print ("Request sent from " + str(proxy[0]+":"+proxy[1]) + " @", self.counter) # print req + counter except: # se nemmeno questo funge, allora il sock e' down print ("Sock down. Retrying request. @", self.counter) s.close() # chiude il socket e ricomincia ciclo
def run(self): # la funzione che da' le istruzioni ai vari threads data = random._urandom(1024) # data per il pacchetto random p = bytes(IP(dst=str(url2))/UDP(dport=int(port))/data) # crea pacchetto udp classico + data current = x # per dare l'id al thread if current < len(proxies): # se l'id del thread si puo' associare ad un proxy, usa quel proxy proxy = proxies[current].strip().split(':') else: # altrimenti lo prende a random proxy = random.choice(proxies).strip().split(":") go.wait() # aspetta che threads siano pronti while True: try: socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5, str(proxy[0]), int(proxy[1]), True) # comando per il proxying con SOCKS s = socks.socksocket() # creazione socket s.connect((str(url2),int(port))) # connessione s.send(p) # invio print ("Request sent from " + str(proxy[0]+":"+proxy[1]) + " @", self.counter) # req + counter except: # se qualcosa va storto questo except chiude il socket e si collega al try sotto s.close() # intanto chiude il precedente socket non funzionante try: socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS4, str(proxy[0]), int(proxy[1]), True) # poi prova ad utilizzare SOCKS4, magari e' questo il problema dell'errore s = socks.socksocket() # creazione socket s.connect((str(url2),int(port))) # connessione s.send(p) # invio print ("Request sent from " + str(proxy[0]+":"+proxy[1]) + " @", self.counter) # req + counter except: # se nemmeno questo funge, allora il sock e' down print ("Sock down. Retrying request. @", self.counter) s.close() # chiude il socket e ricomincia ciclo
def run(self): # la funzione che da' le istruzioni ai vari threads useragent = "User-Agent: " + random.choice(useragents) + "\r\n" # scelta proxy a caso accept = random.choice(acceptall) # scelta accept a caso request = get_host + useragent + accept + connection + "\r\n" # composizione final request current = x # per dare l'id al thread if current < len(proxies): # se l'id del thread si puo' associare ad un proxy, usa quel proxy proxy = proxies[current].strip().split(':') else: # altrimenti lo prende a random proxy = random.choice(proxies).strip().split(":") go.wait() # aspetta che threads siano pronti while True: try: socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5, str(proxy[0]), int(proxy[1]), True) # comando per proxarci con i socks s = socks.socksocket() # creazione socket con pysocks s.connect((str(url2), int(urlport))) # connessione s.send (str.encode(request)) # invio print ("Request sent from " + str(proxy[0]+":"+proxy[1]) + " @", self.counter) # print req + counter except: # se qualcosa va storto questo except chiude il socket e si collega al try sotto s.close() # chiude socket try: # il try prova a vedere se l'errore e' causato dalla tipologia di socks errata, infatti prova con SOCKS4 socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS4, str(proxy[0]), int(proxy[1]), True) # prova con SOCKS4 s = socks.socksocket() # creazione nuovo socket s.connect((str(url2), int(urlport))) # connessione s.send (str.encode(request)) # invio print ("Request sent from " + str(proxy[0]+":"+proxy[1]) + " @", self.counter) # print req + counter except: print ("Sock down. Retrying request. @", self.counter) s.close() # se nemmeno con quel try si e' riuscito a inviare niente, allora il sock e' down e chiude il socket.
def SetDefaultProxy(self): socks.setdefaultproxy()
def set(self): socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS4, self.proxy_server, self.proxy_port, True) socket.socket = socks.socksocket
def get_recv_info(domain, whois_addr, tcpCliSock): # global _ip, _port, _mode HOST = whois_addr data_result = "" PORT = 43 # ?? BUFSIZ = 1024 # ???????? ADDR = (HOST, PORT) EOF = "\r\n" data_send = domain + EOF socks.setdefaultproxy(proxytype = _mode, addr = _ip, port = _port) socket.socket = socks.socksocket try: tcpCliSock.connect(ADDR) tcpCliSock.send(data_send) except socket.error, e: return # if str(e).find("time out") != -1: #???? # return "ERROR -1" # elif str(e).find("Temporary failure in name resolution") != -1: # return "ERROR -2" # else: # return 'ERROR other' while True: try: data_rcv = tcpCliSock.recv(BUFSIZ) except socket.error, e: return if not len(data_rcv): return data_result # ?????? data_result = data_result + data_rcv # ????????
def download(url, tor=False): def create_connection(address, timeout=None, source_address=None): sock = socks.socksocket() sock.connect(address) return sock if tor: if not HAVE_SOCKS: print_error("Missing dependency, install socks (`pip install SocksiPy`)") return None socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5, '127.0.0.1', 9050) socket.socket = socks.socksocket socket.create_connection = create_connection try: req = Request(url) req.add_header('User-agent', 'Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)') res = urlopen(req) data = res.read() except HTTPError as e: print_error(e) except URLError as e: if tor and e.reason.errno == 111: print_error("Connection refused, maybe Tor is not running?") else: print_error(e) except Exception as e: print_error("Failed download: {0}".format(e)) else: return data
def setProxy(self, host, port): int_port = int(port) socks.setdefaultproxy(socks.PROXY_TYPE_HTTP, host, int_port) socket.socket = socks.socksocket self.port = port print 'ipify: ', self.session_get('https://api.ipify.org').content # ------------------------------------------------------------------------- # setTor proxy # -------------------------------------------------------------------------
def setTor(self, port): self.tor = True int_port = int(port) socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5, '127.0.0.1', int_port) socket.socket = socks.socksocket self.port = port self.restartTor() # ------------------------------------------------------------------------- # restartTor # -------------------------------------------------------------------------
def main(): banner() results = [] args = getArguments(sys.argv) print "Start the analyze: " if args.tor == True: socks.setdefaultproxy(proxy_type=socks.PROXY_TYPE_SOCKS5, addr="127.0.0.1", port=9050) socket.socket = socks.socksocket for ip in args.listIps: print "IP: " + ip print "-----------------------" if args.all: dataN = analyzeIP_Neo4j(str(ip),args) if dataN: results.append(dataN) dataO = analyzeIP_Orient(str(ip),args) if dataO: results.append(dataO) dataA = analyzeIP_ArangoDB(str(ip),args) if dataA: results.append(dataA) dataV = analyzeIP_Virtuoso(str(ip),args) if dataV: results.append(dataV) dataAl = analyzeIP_Allegro(str(ip),args) if dataAl: results.append(dataAl) elif args.neo4j: data = analyzeIP_Neo4j(str(ip),args) if data: results.append(data) elif args.orient: data = analyzeIP_Orient(str(ip),args) if data: results.append(data) elif args.arango: data = analyzeIP_ArangoDB(str(ip),args) if data: results.append(data) elif args.virtuoso: data = analyzeIP_Virtuoso(str(ip),args) if data: results.append(data) elif args.allegro: data = analyzeIP_Allegro(str(ip),args) if data: results.append(data) else: print "Error with arguments" if results: print "Writting the results in the output file: " + args.output fileout = open(args.output, "w") json_str = json.dumps(results) fileout.write(json_str) fileout.close() else: print "-----------" print "No results"
def queryJsonRPC(host, port, method): # Connect if host.endswith('.onion'): socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5, 'localhost', 9050, True) s = socks.socksocket() else: s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.settimeout(5) try: s.connect((host, port)) except: print 'Can\'t connect to Electrum' return False # Send request = { 'method': method, 'id': 1 } s.sendall(json.dumps(request) + '\n') # Receive response = '' while True: try: msg = s.recv(1024) response += msg except socket.timeout: print 'Socket timeout' break except socket.error: print 'Socket error' break else: if msg.find('\n') != -1 or len(msg) == 0: break s.close() # Parse try: result = json.loads(response) return result['result'] except: pass return False
def index(request): if request.method == 'POST': # Connection to the DB connection = psycopg2.connect(database="oniongatherer", user='postgres', password='') # Extraction of website and onions address req = json.loads(request.read()) website = req["website"] onionsPost = req["onions"] socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5, "127.0.0.1", 9050, True) dictionary = dict() q = getQueue() for i in range(0, len(onionsPost)): onion = str(onionsPost[i]) cur = connection.cursor() cur.execute("select * from onions where url LIKE (%s)", [onion]); # Insert the tuple ((onion, website)) in the queue iff it was not there, otherwise get the current status if(cur.fetchall() == []): status = '0' q.put((onion, website)) else: cur.execute("select status from onions where url LIKE (%s)", [onion]); status = (cur.fetchall()) status = status[0][0] connection.commit() dictionary[onionsPost[i]] = status data = {} data['onions'] = dictionary json_data = json.dumps(data) connection.close() else: return HttpResponse("debug") return JsonResponse({'onions' : dictionary})