Sinisterly
Gold Proxy Checker (Python script) - Printable Version

+- Sinisterly (https://sinister.ly)
+-- Forum: Computers (https://sinister.ly/Forum-Computers)
+--- Forum: Networking (https://sinister.ly/Forum-Networking)
+---- Forum: Anonymity (https://sinister.ly/Forum-Anonymity)
+---- Thread: Gold Proxy Checker (Python script) (/Thread-Gold-Proxy-Checker-Python-script)

Pages: 1 2


Proxy Checker (Python script) - m0dem - 03-18-2016

I was bored, so I made this Python script that reads a list of proxies from a file and then writes all the still-working proxies to an out file.

Code:
from Queue import Queue from threading import Thread import argparse import requests import sys import time def process_proxy(): try: while True: proxy = queue.get() if proxy == "STOP": return save_valid_proxy(check_proxy(proxy)) queue.task_done() except: pass def check_proxy(proxy, timeout = 5, url = "https://google.com"): proxies = { "http": "http://" + proxy, "https": "https://" + proxy } try: r = requests.get(url, proxies = proxies, timeout = timeout) except IOError: return False return proxy def save_valid_proxy(proxy): if proxy: OUT_F.write(proxy + "\n") if __name__ == "__main__": parser = argparse.ArgumentParser(description = "Check a proxy list for working proxies.") parser.add_argument("infile", help = "The proxy list file.") parser.add_argument("outfile", help = "The output file.") parser.add_argument("-t", "--threads", type = int, help = "Set the number of threads running concurrently.") parser.add_argument("-v", "--verbose", action = "store_true", help = "Be verbose?") args = parser.parse_args() IN_F = open(args.infile, "r") OUT_F = open(args.outfile, "w") # number of threads running at once if args.threads: concurrent_threads = args.threads else: concurrent_threads = 250 if args.verbose: print("Loading proxy list from: {}".format(args.infile)) print("Saving valid proxies list to: {}".format(args.outfile)) print("Running: {} threads...".format(concurrent_threads)) queue = Queue(concurrent_threads * 2) for i in range(0, concurrent_threads): thread = Thread(target = process_proxy) thread.setDaemon(True) thread.start() start = time.time() try: for proxy in IN_F: queue.put(proxy.strip()) # queue.join() except KeyboardInterrupt: pass print("Closing down, please wait. ({} seconds run time)".format(time.time() - start)) queue.put("STOP") IN_F.close() OUT_F.close() sys.exit()

Example usage:
Code:
proxy_checker.py proxylist.txt validproxies.txt --threads 500

If you liked or found this useful, please comment. Smile


RE: Proxy Checker (Python script) - m0dem - 03-19-2016

Proxy Checker v2

Changes:
  • Better proxy checking
  • Bug fixes
  • Changed command-line argument handling code
  • More robust

Code:
''' Proxy Checker v2 -=m0dem=- Requirements: requests (HTTP library) ''' from Queue import Queue from threading import Thread import argparse import requests import sys import time def process_proxy(): try: while True: proxy = queue.get() if proxy == "STOP": return save_valid_proxy(check_proxy(proxy)) queue.task_done() except: pass def check_proxy(proxy, timeout = 5): proxies = { "http": "http://" + proxy, "https": "https://" + proxy } try: # see if the proxy actually works ip = get_external_ip(proxies = proxies) if ip == ORIG_IP: if VERBOSE: print(ip) return False except IOError: return False return proxy def save_valid_proxy(proxy): if proxy: OUT_F.write(proxy + "\n") def get_external_ip(proxies = None): headers = {"User-Agent": "Mozilla/5.0"} if proxies: r = requests.get(IP_CHECK, proxies = proxies, headers = headers) else: r = requests.get(IP_CHECK, headers = headers) return str(r.text) DEFAULT_THREADS = 200 IP_CHECK = "https://api.ipify.org" IN_F = None OUT_F = None VERBOSE = False ORIG_IP = None if __name__ == "__main__": # handle all the command-line argument stuff parser = argparse.ArgumentParser(description = "Check a proxy list for working proxies.") parser.add_argument("infile", type = str, help = "input proxy list file") parser.add_argument("outfile", type = str, help = "output proxy list file") parser.add_argument("-t", "--threads", type = int, default = DEFAULT_THREADS, help = "set the number of threads running concurrently (default {})".format(DEFAULT_THREADS)) parser.add_argument("-v", "--verbose", action = "store_true", help = "say lots of useless stuff (sometimes)") args = parser.parse_args() try: IN_F = open(args.infile, "r") except IOError: print("Invalid proxy list filename.") sys.exit() OUT_F = open(args.outfile, "w") # number of threads running at once CONCURRENT_THREADS = args.threads if args.verbose: VERBOSE = True print("Loading proxy list from: {}".format(args.infile)) print("Saving valid proxies list to: {}".format(args.outfile)) print("Running: {} threads...".format(CONCURRENT_THREADS)) # let's begin the whole process ORIG_IP = get_external_ip() if VERBOSE: print("Your original external IP address is: {}".format(ORIG_IP)) print("Checking proxies...") queue = Queue(CONCURRENT_THREADS * 2) for i in range(0, CONCURRENT_THREADS): thread = Thread(target = process_proxy) thread.daemon = True thread.start() start = time.time() try: for proxy in IN_F: queue.put(proxy.strip()) # queue.join() except KeyboardInterrupt: pass # make sure everything is closed down print("Closing down, please wait. ({} seconds run time)".format(time.time() - start)) queue.put("STOP") IN_F.close() OUT_F.close() sys.exit()

If you found any bugs or just liked my program, please comment.


RE: Proxy Checker (Python script) - Drays - 03-21-2016

Looks cool!

I made a proxy checker in Python too but all I used was the received source, I'm not sure I exactly understand how you are filtering working proxies from non working proxies, would you mind explaining?


RE: Proxy Checker (Python script) - m0dem - 03-21-2016

(03-21-2016, 07:35 PM)Drays Wrote: Looks cool!

I made a proxy checker in Python too but all I used was the received source, I'm not sure I exactly understand how you are filtering working proxies from non working proxies, would you mind explaining?

First, I get the original IP address (without any proxy) and save that as a global variable. (bad me, yes, I know)
I get it by GET requesting https://api.ipify.org. (click on the link, it'll give you your IP without a bunch of other HTML junk -- great for programs that need to check the external IP)
After I know my original IP, I can just loop through the each proxy and check what my external IP is. If it is the same as the original or if it doesn't even work, that proxy is trash.

That's how I do it.


RE: Proxy Checker (Python script) - Inori - 03-22-2016

A few things I'd add:
  • For maximum v2/v3 compatibility, and general code convention, it's common to define methods in your script in order of dependency. I.e.,
    Code:
    def save_valid_proxy(proxy): ... def process_proxy(): ... ... ...
  • chain your imports
    Code:
    import argparse,requests,sys,time
  • don't import unneeded methods and classes; all you need from argparse is the ArgumentParser method
  • making this a library would be cool. Like, one other programmers can import and use, negating the CLI
  • the if __name__=="__main__": ... convention isn't totally needed, a main() method works fine
  • try using packages; it's easier to maintain your code, and easier for other devs to understand
  • publish to PyPI or github to spread it further. (PyPI can create executables for your program, creating a system-wide CLI, as opposed to one you need to run from the directory it's in)



RE: Proxy Checker (Python script) - m0dem - 03-22-2016

(03-22-2016, 03:53 AM)Inori Wrote: A few things I'd add:
  • For maximum v2/v3 compatibility, and general code convention, it's common to define methods in your script in order of dependency. I.e.,
    Code:
    def save_valid_proxy(proxy): ... def process_proxy(): ... ... ...
  • chain your imports
    Code:
    import argparse,requests,sys,time
  • don't import unneeded methods and classes; all you need from argparse is the ArgumentParser method
  • making this a library would be cool. Like, one other programmers can import and use, negating the CLI
  • the if __name__=="__main__": ... convention isn't totally needed, a main() method works fine
  • try using packages; it's easier to maintain your code, and easier for other devs to understand
  • publish to PyPI or github to spread it further. (PyPI can create executables for your program, creating a system-wide CLI, as opposed to one you need to run from the directory it's in)

So, the most dependent function is at the bottom of the list of functions? (for example, main would be at the bottom)

I don't know why, but it's personal preference for me not to chain imports together. I think it looks neater.

I'll admit, the reason I used didn't use a main function is because I wanted to use global variables. (lazy... I'll try to re-structure everything better in v3)

I like your idea of it being a library, not just a CLI.

I need to use Github, but I hardly ever get around to it.

Thanks for the pointers. I'll try to make v3 based on your tips. Smile

@Inori, I setup a GitHub repo for Proxy Checker:
https://github.com/M0dem/Proxy-Checker
As you can see from my commit history... I'm not too handy with Git. (5 commits to get everything right in the repository)


RE: Proxy Checker (Python script) - m0dem - 03-23-2016

For those of you who might be interested, I've pushed Proxy Checker v3 to Github. Been working today on this project.
https://github.com/M0dem/Proxy-Checker
The code is re-structured as a class (and function) that you can use as a library or CLI. Clever, ehh? xD
WARNING: As you might expect... there will be bugs. If you find any, please report them to me. Thanks. Smile
Thanks again for the tips @Inori.


RE: Proxy Checker (Python script) - Drays - 03-24-2016

Hey is this Py34 or py27?


RE: Proxy Checker (Python script) - m0dem - 03-24-2016

(03-24-2016, 12:57 PM)Drays Wrote: Hey is this Py34 or py27?

Python 2.7
Might work for 3, haven't tried.


RE: Proxy Checker (Python script) - Inori - 03-24-2016

(03-23-2016, 06:04 AM)m0dem Wrote: For those of you who might be interested, I've pushed Proxy Checker v3 to Github. Been working today on this project.
https://github.com/M0dem/Proxy-Checker
The code is re-structured as a class (and function) that you can use as a library or CLI. Clever, ehh? xD
WARNING: As you might expect... there will be bugs. If you find any, please report them to me. Thanks. Smile
Thanks again for the tips @Inori.

Bah, damn you, I was about to submit a pull request, but then you added the library Tongue