Взаимная передача данных (программирование сокетов Python)
Hello. I'm a beginner trying to finish one project concerning networking. I'm working on a server-client duo, and i found a lot of obstacles. Most of them caused problems to me only due to my complete lack of knowledge and experience, so i guess that's the case also this time. I've managed to create a server which can send commands to a client. Client is currently able to execute a command, check if it was executed correctly, but now i try to send a response to a server, and that's where i have troubles. I can't figure how to do it. I will post my code down below. I would be immensely grateful for any sort of help. I'd like to know how to force it to send and recieve a response. I'm sorry for wasting your times for such trivialities, but i hope someone will want to show me the right direction. Thank you in advance and have a nice day.
Что я уже пробовал:
Сервер:
import socket def socket_create(): try: global host global port global s host = '' port = 9999 s = socket.socket() except socket.error as msg: print("Socket creation error: " + str(msg)) print("Test server") def socket_bind(): try: global host global port global s s.bind((host, port)) print("Binding...") s.listen(5) except socket.error as msg: print("Binding error: " + str(msg) + "\n" + "Retrying...") socket.bind() def socket_accept(): conn, address = s.accept() print("Connection established | " + "IP " + address[0] + " | Port " + str(address[1])) print("Give the command: ") send_command(conn) receive_command() #try conn.close() def send_command(conn): while True: cmd = input() if len(str.encode(cmd)) > 0: conn.send(str.encode(cmd)) #try def receive_command(): while True: resp = s.recv(1024) if resp[:1].decode("ISO-8859-1") == 'e': print("test") def main(): socket_create() socket_bind() socket_accept() main()
Клиент:
import pyautogui import os import socket import threading import time s = socket.socket() host = '' #place for ip port = 9999 s.connect((host, port)) print("Test client") path = 'D:/screen' os.chdir(path) def sender1(): conn, address = s.accept() responder1(conn) conn.close() def printer1(): pyautogui.screenshot('D:/screen/screenshot1.png') if os.path.isfile('./screenshot1.png'): print("Screenshot1 was created") else: print("Screenshot1 was not created") def printer2(): pyautogui.screenshot('D:/screen/screenshot2.png') if os.path.isfile('./screenshot2.png'): print("Screenshot2 was created") else: print("Screenshot2 was not created") def responder1(): if os.path.isfile('./screenshot1.png'): s.send(str.encode("e")) def responder2(): if os.path.isfile('./screenshot2.png'): print("test 2") else: print("n. test 2") t1 = threading.Thread(target=printer1, name='thread1') t2 = threading.Thread(target=printer2, name='thread2') t3 = threading.Thread(target=responder1, name='thread3') t4 = threading.Thread(target=responder2, name='thread4') while True: command = s.recv(1024) print(command) #delete after test if command[:1].decode("ISO-8859-1") == 'a': t1.start() time.sleep(2) t3.start() if command[:1].decode("ISO-8859-1") == 'b': t2.start() time.sleep(2) t4.start() s.close()