Answer the question
In order to leave comments, you need to log in
How to pass multiple numbers from client to server and return their sum?
Hello!
Launched such a local server
import socket
sock = socket.socket()
sock.bind(('', 9090))
sock.listen(5)
connenction = sock.accept()
address = sock.accept()
print('connected:', addres)
while True:
data = connect.recv(1024) #хватаю тестовое послание
#a = connect.recv(1024)
#b = connect.recv(1024)
#do = connectrecv(1024).encode() #неработающая часть, хочу посылать" a,b,действие"
#(тобишь посылаю "1,2,+" и на клиенте получаю 3 - но ничего не возвращает
#data_1 = (a+b)
#sock.send(data_1)
if not data:
print("пусто")
break
conn.send(data.upper())
conn.close()
import socket
sock = socket.socket()
sock.connect(('localhost', 9090))
sock.send("hello, world!".encode()) #тестовое послание
a = input("a= ")
sock.send(a.encode())
b = input("b= ")
sock.send(b.encode())
#do = input("+ - / * ")
#sock.send(do.encode())
#do = "+".encode() не сработало
#sock.send(do)
data = sock.recv(1024)
sock.close()
print(data.decode())
Answer the question
In order to leave comments, you need to log in
Sketched on the knee.
Server:
import socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_address = ('localhost', 9090)
print('starting up on {} port {}'.format(*server_address))
sock.bind(server_address)
sock.listen(1)
while True:
print('waiting for a connection')
connection, client_address = sock.accept()
try:
print('connection from', client_address)
while True:
data = connection.recv(1024)
print('received {!r}'.format(data))
if data:
print('sending data back to the client')
input_data = (data.decode())
expression = input_data[0] + input_data[2] + input_data[1]
result = eval(expression)
connection.sendall(str(result).encode())
else:
print('no data from', client_address)
break
finally:
connection.close()
import socket
sock = socket.socket()
sock.connect(('localhost', 9090))
a = input("a= ")
b = input("b= ")
do = input("+ - / * ")
message = a + b + do
sock.send(message.encode())
data = sock.recv(1024)
res = data.decode()
print(res)
sock.close()
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question