Best Python code snippet using autotest_python
simple_web_server_with_epoll.py
Source:simple_web_server_with_epoll.py
...17 self.sock.setblocking(0)18 self.sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)19 print "Started Epoll Server"20 self.epoll = select.epoll()21 self.epoll.register(self.sock.fileno(), select.EPOLLIN)22 def run(self):23 """Executes epoll server operation"""24 try:25 connections = {}; requests = {}; responses = {}26 while True:27 events = self.epoll.poll(1)28 for fileno, event in events:29 if fileno == self.sock.fileno():30 connection, address = self.sock.accept()31 connection.setblocking(0)32 self.epoll.register(connection.fileno(), select.EPOLLIN)33 connections[connection.fileno()] = connection34 requests[connection.fileno()] = b''35 responses[connection.fileno()] = SERVER_RESPONSE36 elif event & select.EPOLLIN:37 requests[fileno] += connections[fileno].recv(1024)38 if EOL1 in requests[fileno] or EOL2 in requests[fileno]:39 self.epoll.modify(fileno, select.EPOLLOUT)40 print '-'*40 + '\n' + requests[fileno].decode()[:-2]41 elif event & select.EPOLLOUT:42 byteswritten = connections[fileno].send(responses[fileno])43 responses[fileno] = responses[fileno][byteswritten:]44 if len(responses[fileno]) == 0:45 self.epoll.modify(fileno, 0)46 connections[fileno].shutdown(socket.SHUT_RDWR)47 elif event & select.EPOLLHUP:48 self.epoll.unregister(fileno)49 connections[fileno].close()50 del connections[fileno]51 finally:52 self.epoll.unregister(self.sock.fileno())53 self.epoll.close()54 self.sock.close()55if __name__ == '__main__':56 parser = argparse.ArgumentParser(description='Socket Server Example With Epoll')57 parser.add_argument('--port', action='store', dest='port', type=int, required=True)58 given_args = parser.parse_args()59 port = given_args.port60 server = EpollServer(host=SERVER_HOST, port=port)...
server_epoll_et.py
Source:server_epoll_et.py
...9serversocket.bind(('0.0.0.0', 8080))10serversocket.listen(1)11serversocket.setblocking(0)12epoll = select.epoll()13epoll.register(serversocket.fileno(), select.EPOLLIN | select.EPOLLET)14try:15 connections = {}; requests = {}; responses = {}16 while True:17 events = epoll.poll(1)18 for fileno, event in events:19 if fileno == serversocket.fileno():20 try:21 while True:22 connection, address = serversocket.accept()23 connection.setblocking(0)24 epoll.register(connection.fileno(), select.EPOLLIN | select.EPOLLET)25 connections[connection.fileno()] = connection26 requests[connection.fileno()] = b''27 responses[connection.fileno()] = response28 except socket.error:29 pass30 elif event & select.EPOLLIN:31 try:32 while True:33 requests[fileno] += connections[fileno].recv(1024)34 except socket.error:35 pass36 if EOL1 in requests[fileno] or EOL2 in requests[fileno]:37 epoll.modify(fileno, select.EPOLLOUT | select.EPOLLET)38 print('-'*40 + '\n' + requests[fileno].decode()[:-2])39 elif event & select.EPOLLOUT:40 try:41 while len(responses[fileno]) > 0:42 byteswritten = connections[fileno].send(responses[fileno])43 responses[fileno] = responses[fileno][byteswritten:]44 except socket.error:45 pass46 if len(responses[fileno]) == 0:47 epoll.modify(fileno, select.EPOLLET)48 connections[fileno].shutdown(socket.SHUT_RDWR)49 elif event & select.EPOLLHUP:50 epoll.unregister(fileno)51 connections[fileno].close()52 del connections[fileno]53finally:54 epoll.unregister(serversocket.fileno())55 epoll.close()...
server_epoll_lt.py
Source:server_epoll_lt.py
...9serversocket.bind(('0.0.0.0', 8080))10serversocket.listen(100) # test check diff 1/10/100/100011serversocket.setblocking(0)12epoll = select.epoll()13epoll.register(serversocket.fileno(), select.EPOLLIN)14try:15 connections = {}; requests = {}; responses = {}16 while True:17 events = epoll.poll(1)18 for fileno, event in events:19 if fileno == serversocket.fileno():20 connection, address = serversocket.accept()21 connection.setblocking(0)22 epoll.register(connection.fileno(), select.EPOLLIN)23 connections[connection.fileno()] = connection24 requests[connection.fileno()] = b''25 responses[connection.fileno()] = response26 elif event & select.EPOLLIN:27 requests[fileno] += connections[fileno].recv(1024)28 if EOL1 in requests[fileno] or EOL2 in requests[fileno]:29 epoll.modify(fileno, select.EPOLLOUT)30 print('-'*40 + '\n' + requests[fileno].decode()[:-2])31 elif event & select.EPOLLOUT:32 byteswritten = connections[fileno].send(responses[fileno])33 responses[fileno] = responses[fileno][byteswritten:]34 if len(responses[fileno]) == 0:35 epoll.modify(fileno, 0)36 connections[fileno].shutdown(socket.SHUT_RDWR) # ä¼é
å
³éï¼è®© client å
å
³ï¼37 elif event & select.EPOLLHUP:38 epoll.unregister(fileno)39 connections[fileno].close()40 del connections[fileno]41finally:42 epoll.unregister(serversocket.fileno())43 epoll.close()...
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!!