Created
April 5, 2020 14:20
-
-
Save amon-ra/d31ca4def7862bf41bc30787ba49d3cc to your computer and use it in GitHub Desktop.
Web video caster redirector program
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python2 | |
import socket, threading | |
import SimpleHTTPServer | |
import SocketServer | |
import sys | |
delay = 0.2 | |
prefix = '192.168.2.' | |
first_ip = 2 | |
last_ip = 200 | |
port = 30001 | |
remote_path = '/web-receiver/index.html' | |
server_port = 30000 | |
def TCP_connect(ip, port_number, delay, output): | |
# print("STart Thread: "+ip) | |
TCPsock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
TCPsock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) | |
TCPsock.settimeout(delay) | |
if not output.get(ip): | |
output[ip] = {} | |
try: | |
TCPsock.connect((ip, port_number)) | |
output[ip][port_number] = 'Listening' | |
except Exception as e: | |
print(e) | |
print(ip) | |
output[ip][port_number] = '' | |
def scan_ports(delay): | |
threads = [] # To run TCP_connect concurrently | |
output = {} # For printing purposes | |
# Spawning threads to scan ports | |
for i in range(first_ip,last_ip): | |
t = threading.Thread(target=TCP_connect, args=(prefix+str(i), port, delay, output)) | |
threads.append(t) | |
# Starting threads | |
for i in range(last_ip-first_ip): | |
threads[i].start() | |
# Locking the main thread until all threads complete | |
for i in range(last_ip-first_ip): | |
threads[i].join() | |
# Printing listening ports from small to large | |
# return ip | |
# print(threads) | |
# print(output) | |
for i in range(first_ip, last_ip): | |
try: | |
if output[prefix+str(i)][port] == 'Listening': | |
return prefix+str(i) | |
except Exception as e: | |
# print(e) | |
pass | |
def redirect_handler_factory(): | |
""" | |
Returns a request handler class that redirects to supplied `url` | |
""" | |
class RedirectHandler(SimpleHTTPServer.SimpleHTTPRequestHandler): | |
def do_GET(self): | |
print('Start scann') | |
ip = scan_ports(delay) | |
if not ip: | |
url = 'http://cast2tv.app' | |
else: | |
url = 'http://'+ip+':'+str(port)+remote_path | |
print(url) | |
self.send_response(302) | |
self.send_header('Location', url) | |
self.end_headers() | |
return RedirectHandler | |
# def main(): | |
# host_ip = input("Enter host IP: ") | |
# delay = int(input("How many seconds the socket is going to wait until timeout: ")) | |
# scan_ports(delay) | |
if __name__ == "__main__": | |
redirectHandler = redirect_handler_factory() | |
handler = SocketServer.TCPServer(('0.0.0.0', server_port), redirectHandler) | |
print("serving at port %s" % server_port) | |
handler.serve_forever() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This creates a web server that when you connect from the TV it finds IP where Web Video Caster is running and redirect to it.
I have it running in a Openwrt Server