Skip to content

Instantly share code, notes, and snippets.

@erm3nda
Last active May 20, 2020 20:02
Show Gist options
  • Save erm3nda/804f9b1e9d6662fe680110e1049be815 to your computer and use it in GitHub Desktop.
Save erm3nda/804f9b1e9d6662fe680110e1049be815 to your computer and use it in GitHub Desktop.
Redirecting http connections with Flask in Python
#!/usr/local/env python
#!/ -*- coding: utf-8 -*-
# Acts as a proxy for the requests, that's all, but instead of proxify them performs a redirection
# Redirection is set to http://anti-captcha.com/in.php
import os
import traceback
import time
from flask import Flask, redirect
app = Flask(__name__)
# If you have both 80 and 8080 ports in use, feel free to edit port number
global port
port=80 # port used by GSA-Anticaptcha and Captcha-Sniper
def portToggle():
port=8080 # another port used so often
def killPort():
print "Something went wrong"
print "Killing app binding port " + str(port)
try:
os.system("sudo fuser -k " + str(port) + "/tcp") # if can't kill current server use 8080
except:
traceback.print_exc()
portToggle()
# TODO: manage hosts file to bypass every captcha service url
@app.route('/', methods=['POST', 'GET'], defaults={'path': ''})
@app.route('/<path:path>')
def catch_all(path):
# return 307 temp redirection to avoid POST calls to being tranformed to GET
return redirect("https://anti-captcha.com/in.php", code=307)
def main():
try:
print "Running flask webserver on port " + str(port)
app.run(host='0.0.0.0', port=port)
time.sleep(2)
except:
#traceback.print_exc()
killPort()
print "Relaunching " + __name__ + "app"
time.sleep(2)
main()
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment