Skip to content

Commit

Permalink
fix: right way to print strings and to set CORs on Python
Browse files Browse the repository at this point in the history
  • Loading branch information
luandro committed Jun 22, 2022
1 parent 2e25c8f commit eb1d1a1
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
20 changes: 15 additions & 5 deletions captive-portal/captive-portal.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,24 @@
'''
class CaptivePortal(SimpleHTTPRequestHandler):
'''
https://stackoverflow.com/questions/16583827/cors-with-python-basehttpserver-501-unsupported-method-options-in-chrome
'''
def do_OPTIONS(self):
self.send_response(200, "ok")
self.send_header('Access-Control-Allow-Origin', '*')
self.send_header('Access-Control-Allow-Methods', 'GET, OPTIONS')
self.send_header("Access-Control-Allow-Headers", "X-Requested-With")
self.send_header("Access-Control-Allow-Headers", "Content-Type")
self.end_headers()
'''
this is called when the user submits the login form
'''
def do_POST(self):
self.send_response(200)
self.send_header("Content-type", "application/json")
# self.send_header('Access-Control-Allow-Origin', '*')
# self.send_header('Access-Control-Allow-Headers', 'Authorization, Content-Type')
# self.send_header('Access-Control-Allow-Methods', 'POST')
self.send_header("Content-Type", "application/json")
self.send_header('Access-Control-Allow-Origin', '*')
self.send_header('Access-Control-Allow-Headers', 'Authorization, Content-Type')
self.send_header('Access-Control-Allow-Methods', 'POST')
self.end_headers()
form = cgi.FieldStorage(
fp=self.rfile,
Expand Down Expand Up @@ -78,7 +88,7 @@ def do_POST(self):
subprocess.call(["iptables", "-A", "FORWARD", "-i", IFACE, "-j" ,"DROP"])
print "Starting web server"
httpd = TCPServer(('', PORT), CaptivePortal)
print 'started httpserver on'+PORT
print 'started httpserver on'+str(PORT)
print "Redirecting HTTP traffic to captive portal"
subprocess.call(["iptables", "-t", "nat", "-A", "PREROUTING", "-i", IFACE, "-p", "tcp", "--dport", "80", "-j" ,"DNAT", "--to-destination", IP_ADDRESS+":"+str(RE_PORT)])

Expand Down
2 changes: 1 addition & 1 deletion captive-portal/redirect.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def do_GET(self):
self.wfile.write(self.html_redirect)

httpdRe = HTTPServer(('', RE_PORT), Redirect)
print 'started httpserver on'+RE_PORT
print 'started httpserver on'+str(RE_PORT)

try:
httpdRe.serve_forever()
Expand Down

0 comments on commit eb1d1a1

Please sign in to comment.