Help with socket client / server with math interactions of sum in python

2

I am studying about socket and wanted to make a socket that when the client types 5 and another 5 the server responds 10 and returns the response to the client. How do I change my code to have this interaction?

** file socket_server.py

#!/usr/bin/env python

from socket import *
from time import time, ctime

HOST = 'localhost'
PORT = 21567
BUFSIZ = 1024
ADDR = (HOST, PORT)

tcpSerSock = socket(AF_INET, SOCK_STREAM)
tcpSerSock.bind(ADDR)
tcpSerSock.listen(5)

while 1:
print ("waiting for connection...")
tcpClisock, addr = tcpSerSock.accept()
print ("...connected from:"), addr

while 1:
data = tcpClisock.recv(BUFSIZ)
print ("[+] %s") % (data)
if data=="exit": 
exit
tcpClisock.send("[SERVER]vc digitou "+data) 

tcpClisock.close()
exit
tcpSerSock.close()

** file socket_client.py

#!/usr/bin/env python 

from socket import * 


HOST = 'localhost' 
PORT = 21567 
BUFSIZ = 1024 
ADDR = (HOST, PORT) 

tcpCliSock = socket(AF_INET, SOCK_STREAM)
tcpCliSock.connect(ADDR) 

while 1: 
data = raw_input("CLIENT > ")
print ("[CLIENT] vc digitou %s") % (data) 
if not data: 
break
tcpCliSock.send(data) 
data = tcpCliSock.recv(1024) 

if not data: 
break 
print (data) 

tcpCliSock.close()
    
asked by anonymous 21.09.2016 / 04:14

1 answer

0

You can declare a global variable on the server ( socket_servidor.py ) to serve as a counter:

soma = 0
...
if data == "exit":
    exit
else
    soma += data
    print(soma)
    
13.10.2016 / 19:03