Best Python code snippet using autotest_python
client.py
Source:client.py
1import socket2import struct3import datetime4import random5import time6VERSION_ENCODE = 'utf-8'7BUFFER_SIZE = 10248def diff_time(time_begin, time_end):9 r = 010 h = 011 m = 012 try:13 milliseconds = int(time_end[9:]) - int(time_begin[9:])14 if milliseconds < 0:15 milliseconds *= -116 seconds = int(time_end[6:8]) - int(time_begin[6:8])17 if seconds < 0:18 seconds += 6019 m = 120 minutes = int(time_end[3:5]) - int(time_begin[3:5]) - m21 if minutes < 0:22 minutes += 6023 h = 124 hours = int(time_end[0:2]) - int(time_begin[0:2]) - h25 if hours < 0:26 hours *= -127 r = 128 return [hours, minutes, seconds, milliseconds, r]29 except ValueError:30 return []31def run(server_ip='10.0.2.15', server_port=1236):32 n = 333 cnt = 034 while n:35 try:36 cnt = int(input("Entrez le nombre de fois que vous voulez avoir l'heure du serveur: "))37 if cnt < 1:38 cnt = 139 n = 040 except ValueError:41 n = n - 142 if n > 0:43 print("La valeur entré n'est pas un entier. Veuillez essayer à nouveau.")44 else:45 print("Nombre d'essaie atteint. Valeur par défaut = 3")46 cnt = 347 df = []48 client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)49 print("Tentative de connection au serveur...")50 client_socket.connect((server_ip, server_port))51 print("Connection au serveur réussit.")52 print()53 print("Envoie du nombre de fois que l'on veut l'heure du serveur au serveur")54 client_socket.send(struct.pack('>i', cnt))55 for i in range(0, cnt):56 time.sleep(random.random() * 10)57 print(f"Demande n°{i} au serveur...")58 time_send = datetime.datetime.now().time()59 client_socket.send("SEND HOUR".encode(VERSION_ENCODE))60 time_received = client_socket.recv(BUFFER_SIZE).decode(VERSION_ENCODE)61 df.append(diff_time(time_received, str(time_send)))62 client_socket.close()63 print("Communication avec le serveur terminé")64 print()65 return df66if __name__ == '__main__':67 t = run()68 print("Différence d'heure avec le serveur enregistré:")69 for d in t:70 if not d:71 pass72 else:...
server.py
Source:server.py
1import socket2import threading3import struct4import datetime5VERSION_ENCODE = 'utf-8'6BUFFER_SIZE = 10247SERVER_LISTEN = 58def handle_client(server_socket):9 client_socket, address = server_socket.accept()10 (cnt, ) = struct.unpack('>i', client_socket.recv(BUFFER_SIZE))11 if not cnt:12 client_socket.close()13 while cnt:14 message = client_socket.recv(BUFFER_SIZE).decode(VERSION_ENCODE)15 t = datetime.datetime.now().time()16 client_socket.sendall(str(t).encode(VERSION_ENCODE))17 cnt = cnt - 118 client_socket.close()19def run(server_port=1236):20 server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)21 server_socket.bind(('', server_port))22 server_socket.listen(SERVER_LISTEN)23 print("Serveur pret!")24 for i in range(SERVER_LISTEN):25 threading.Thread(target=handle_client, args=(server_socket, )).start()26if __name__ == '__main__':...
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!!