def main(): server = Server(MyOneServer, PORT) server.start() abspath = os.path.abspath(__file__) dname = os.path.dirname(abspath) os.chdir(dname) potential_ip = getLocalIP() imgs = [] class ShowImgThread(Thread): def __init__(self, img): Thread.__init__(self) self.img = img def run(self): self.img.show() potential_ip = {*potential_ip} - {'192.168.56.1'} # Cisco Anyconnect shit for ip in potential_ip: addr = 'http://%s:%d' % (ip, PORT) try: imgs.append(qrcode.make(addr)) except Exception as e: print('Error 198456', e) print(addr) [ShowImgThread(x).start() for x in imgs] try: input() except KeyboardInterrupt: print('Received ^C. ') finally: server.close() server.join()
def connect(): global server server = socket() server.settimeout(1) server.bind(('', PORT)) server.listen(1) local_ip = getLocalIP() print('My IP is one of these:', local_ip) print('port:', PORT) while True: try: s, addr = server.accept() break except: pass print('incoming connection from', addr) if addr[0] in TRUSTED_IP or addr[0] in local_ip: print('IP is trusted. ') else: op = 'big pink' while op not in 'yn': op = input('IP not in whitelist. Accept? y/n > ') if op != 'y': print("Aborted. Let's start over. ") try: s.close() except: pass server.close() return connect() print('Nice! ') return s
def main(): ips = getLocalIP() ips = [x for x in ips if not x.startswith('192.')] for ip in ips: try: print('You are at:', ip, entries[ip]) except KeyError: print('unknown IP:', ip) print() print('explorer', '\\'.join(__file__.split('\\')[:-1])) input('Enter...')
def main(): global professional if len(sys.argv) == 2 and sys.argv[1] == '!': print('Professional mode. ') professional = True s = PickleSocket() print('My IP =', gethostbyname(gethostname())) print('or...', getLocalIP()) server = None try: print('Server or Client? s/c') if listen((b's', b'c')) == b's': s.bind(('', port)) s.listen(1) print('Listening...') server = s s, addr = server.accept() print('Connection from', addr) print('Accept? y/n') if listen((b'y', b'n')) == b'n': s.close() s = None input('Aborted. Enter to exit...') sys.exit(1) else: ip = input('Connect to IP: ') print('Connecting...') s.connect((ip, port)) print('Connected! Waiting for response... ') s.shakeHands('One ship python by Daniel Chin. ') esc = b'\x1b' op = b'' while op != esc: if op == b's': send(s) elif op == b'r': #Cannot use ELSE, cuz op maybe b''. recv(s) print() print('Send, receive, or exit? s/r/esc') op = listen((b's', b'r', esc)) finally: if s: s.close() if server: server.close()
def main(): server = MyServer(MyOneServer, PORT) server.start() abspath = os.path.abspath(__file__) dname = os.path.dirname(abspath) os.chdir(dname) potential_ip = getLocalIP() imgs = [] class ShowImgThread(Thread): def __init__(self, img): Thread.__init__(self) self.img = img def run(self): self.img.show() potential_ip = {*potential_ip} - {'192.168.56.1'} # Cisco Anyconnect shit for ip in potential_ip: addr = 'http://%s:%d' % (ip, PORT) try: imgs.append(qrcode.make(addr)) except Exception as e: print('Error 198456', e) print(addr) print('Ctrl+C to stop.') print('Q to display QR code for phone scan.') try: while True: op = listen(timeout=1) if op == b'\x03': raise KeyboardInterrupt elif op == b'q': [ShowImgThread(x).start() for x in imgs] print('Loading image.') elif op == b'\r': print() except KeyboardInterrupt: print('Received ^C. ') finally: server.close() server.join()
def main(Handler, app_name = 'Safe HTTP', log_dir = None, **kw): ''' `Handler` is a class whose following methods you can override: __init__(self, request, sock, addr, time_out, debug, info, warning): Should be non-blocking. prepareResponse(self, debug, info, warning): Prepare the `self.response_content`. Set `self.content_length`. Should return sooner than `self.time_out`. Return 'wait' to indicate job not done yet. You will be re-elected to execute `prepareResponse()`. Return 'done' to indicate job complete. Call unlink yourself. Return 'close' to demand the socket to be closed and removed. Risk of spinlock: The main loop try-waits. Do not do nothing and return 'wait'. unlink(self): called to release files. ''' terminals, debug, info, warning = initTerminals(app_name, log_dir) try: info.print('socket read is maxed at', PAGE // 1024, 'KB, allowing', TOP_SPEED[1], '/ s under ACCEPT_TIMEOUT =', ACCEPT_TIMEOUT, 's.') loadOptions(kw, info) serverSock = socket() serverSock.bind(('', options['port'])) serverSock.listen(LISTEN) local_ip = getLocalIP() if not local_ip: local_ip = [gethostname()] local_ip.append(gethostbyname(local_ip[0])) info.print('listening at', local_ip, '...') try: loop(serverSock, Handler, debug, info, warning) except KeyboardInterrupt: RECEIVED_CTRL_C = 'Received ^C. ' info.print(RECEIVED_CTRL_C) print(RECEIVED_CTRL_C) finally: force(serverSock.close, debug, info, warning) info.print('Exiting main(). ') except Exception as e: warning.exception() raise e