Question: this is the code for server.py import socket import json with open('htp.json')as f: data = json.load(f) for state in data['state']: x = state['sel'] # create
this is the code for server.py
import socket
import json
with open('htp.json')as f:
data = json.load(f)
for state in data['state']:
x = state['sel']
# create the socket
# AF_INET == ipv4
# SOCK_STREAM == TCP
HEADERSIZE = 10
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((socket.gethostname(), 1235))
while True:
# now our endpoint knows about the OTHER endpoint.
clientsocket, address = s.accept()
print(f"Connection from {address} has been established.")
msg="welcom to the server"
msg=f'{len(msg):<{HEADERSIZE}}'+msg
clientsocket.send(bytes(msg,"utf-8"))
this is the code foe client.py
import socket
HEADERSIZE = 10
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((socket.gethostname(), 1235))
while True:
full_msg = ''
new_msg = True
while True:
msg = s.recv(16)
if new_msg:
print("new data:",msg[:HEADERSIZE])
msglen = int(msg[:HEADERSIZE])
new_msg = False
full_msg += msg.decode("utf-8")
if len(full_msg)-HEADERSIZE == msglen:
print("data recvd")
print(full_msg[HEADERSIZE:])
new_msg = True
full_msg = ''
print(full_msg)
this is what is in htp.json
{"state":[{"sel":1}]}
I want to pass the vale 1 from the json file to the clinet useing the server
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
