Ejemplo 1: cliente tcp usando sockets en python
from socket import *
import sys
import json
serverName = ""
serverPort = 6100
clientSocket = socket(AF_INET, SOCK_STREAM)
clientSocket.connect((serverName, serverPort))
sample_data = {
"Aparna" : 1,
"Pooja" : 2,
"Shreya" : 3,
"Tanishq" : 4
}
serialized_data = json.dumps(sample_data) #data serialized
# clientSocket.send(str.encode(sample_data))
clientSocket.send(str.encode(serialized_data))
response_data = clientSocket.recv(1024)
print("Response data from server : ", response_data.decode())
clientSocket.close()
Ejemplo 2: servidor tcp usando sockets en python
import socket
from socket import *
import json
serverPort = 6100
serverSocket = socket(AF_INET, SOCK_STREAM)
# Avoiding : Python [Errno 98] Address already in use
serverSocket.bind(("", serverPort))
serverSocket.listen(1)
# TCP Server
print("The server is listening on PORT 6100")
while 1:
connectionSocket, addr = serverSocket.accept()
data = connectionSocket.recv(1024)
# De-serializing data
data_loaded = json.loads(data)
print("client Sent :n ", data_loaded)
# Sending response
connectionSocket.send(str.encode("Mil gya data"))
connectionSocket.close()
¡Haz clic para puntuar esta entrada!
(Votos: 0 Promedio: 0)