Best Python code snippet using localstack_python
fail.py
Source:fail.py
...54 LOGGER.debug('Dst: {}'.format(dst))55 56 proxy_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)57 #proxy_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)58 proxy_socket.bind(ip_to_tuple(src))59 60 client_address = ('192.168.31.234',27960)61 server_address = ip_to_tuple(dst)62 #s = conf.L3socket(iface='en0')63 64 LOGGER.debug('Looping proxy (press Ctrl-Break to stop)...')65 while True:66 #for i in range( 10 ):67 #print i68 data, address = proxy_socket.recvfrom(BUFFER_SIZE)69 #print address70 71 if client_address == None:72 client_address = address73 if address == client_address:74 print 'to server'75 #print data76 data = LOCAL_DATA_HANDLER(data)77 proxy_socket.sendto(data, server_address)78 #s.send( IP( dst = "192.168.31.49" , src = '192.168.31.160' ) / UDP( dport = 27960 , sport = 27960 ) / Raw( load = data ) )79 elif address == server_address:80 #print 'to client'81 #print data82 data = REMOTE_DATA_HANDLER(data)83 #s.send( IP( dst = "192.168.31.234" , src = '192.168.31.160' ) / UDP( dport = 27960 , sport = 27960 ) / Raw( load = data ) )84 proxy_socket.sendto(data, client_address)85 #client_address = None86 else:87 LOGGER.warning('Unknown address: {}'.format(str(address)))88 89def ip_to_tuple(ip):90 """Parse IP string and return (ip, port) tuple.91 92 Arguments:93 ip -- IP address:port string. I.e.: '127.0.0.1:8000'.94 """95 ip, port = ip.split(':')96 return (ip, int(port))97# end-of-function ip_to_tuple98def main():99 """Main method."""100 parser = argparse.ArgumentParser(description='TCP/UPD proxy.')101 102 # TCP UPD groups103 proto_group = parser.add_mutually_exclusive_group(required=True)...
pyproxy.py
Source:pyproxy.py
...28 LOGGER.debug('Src: {}'.format(src))29 LOGGER.debug('Dst: {}'.format(dst))30 31 proxy_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)32 proxy_socket.bind(ip_to_tuple(src))33 34 client_address = None35 server_address = ip_to_tuple(dst)36 37 LOGGER.debug('Looping proxy (press Ctrl-Break to stop)...')38 while True:39 data, address = proxy_socket.recvfrom(BUFFER_SIZE)40 41 if client_address == None:42 client_address = address43 if address == client_address:44 data = LOCAL_DATA_HANDLER(data)45 proxy_socket.sendto(data, server_address)46 elif address == server_address:47 data = REMOTE_DATA_HANDLER(data)48 proxy_socket.sendto(data, client_address)49 client_address = None50 else:51 LOGGER.warning('Unknown address: {}'.format(str(address)))52# end-of-function udp_proxy 53 54 55def tcp_proxy(src, dst):56 """Run TCP proxy.57 58 Arguments:59 src -- Source IP address and port string. I.e.: '127.0.0.1:8000'60 dst -- Destination IP address and port. I.e.: '127.0.0.1:8888'61 """62 LOGGER.debug('Starting TCP proxy...')63 LOGGER.debug('Src: {}'.format(src))64 LOGGER.debug('Dst: {}'.format(dst))65 66 sockets = []67 68 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)69 s.bind(ip_to_tuple(src))70 s.listen(1)71 s_src, _ = s.accept()72 s_dst = socket.socket(socket.AF_INET, socket.SOCK_STREAM)73 s_dst.connect(ip_to_tuple(dst)) 74 75 sockets.append(s_src)76 sockets.append(s_dst)77 78 while True:79 s_read, _, _ = select.select(sockets, [], [])80 81 for s in s_read:82 data = s.recv(BUFFER_SIZE)83 84 if s == s_src:85 d = LOCAL_DATA_HANDLER(data)86 s_dst.sendall(d)87 elif s == s_dst:88 d = REMOTE_DATA_HANDLER(data)89 s_src.sendall(d)90# end-of-function tcp_proxy 91def ip_to_tuple(ip):92 """Parse IP string and return (ip, port) tuple.93 94 Arguments:95 ip -- IP address:port string. I.e.: '127.0.0.1:8000'.96 """97 ip, port = ip.split(':')98 return (ip, int(port))99# end-of-function ip_to_tuple100def main():101 """Main method."""102 parser = argparse.ArgumentParser(description='TCP/UPD proxy.')103 104 # TCP UPD groups105 proto_group = parser.add_mutually_exclusive_group(required=True)...
proxy_server.py
Source:proxy_server.py
...14 """15 src = "%s:%s" % (BIND_HOST, src) if is_number(src) else src16 dst = "%s:%s" % (LOCALHOST_IP, dst) if is_number(dst) else dst17 thread = kwargs.get("_thread")18 def ip_to_tuple(ip):19 ip, port = ip.split(":")20 return ip, int(port)21 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)22 s.bind(ip_to_tuple(src))23 s.listen(1)24 s.settimeout(10)25 def handle_request(s_src, thread):26 s_dst = socket.socket(socket.AF_INET, socket.SOCK_STREAM)27 s_dst.connect(ip_to_tuple(dst))28 sockets = [s_src, s_dst]29 try:30 while thread.running:31 s_read, _, _ = select.select(sockets, [], [])32 for s in s_read:33 data = s.recv(BUFFER_SIZE)34 if data in [b"", "", None]:35 return36 if s == s_src:37 forward, response = data, None38 if handler:39 forward, response = handler(data)40 if forward is not None:41 s_dst.sendall(forward)...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!