Commit 785573e9 by source_reader

added tcp server and client

parent 93e982cf
import socket
import json
from binascii import b2a_hex
# create Socket object (IP, PORT)
ADDRESS = ('127.0.0.1', 1234)
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# connect to a server at IP:PORT
sock.connect(ADDRESS)
conn = sock.makefile('rwb')
print("Connected to [%s:%s]" % (ADDRESS[0], ADDRESS[1]))
# Data to send to server (change this)
tx_obj = [8, 10, -12, 100, 2]
print("Sending %s to server" % type(tx_obj))
# L3: convert Python object into string
tx_str = json.dumps(tx_obj)
print("\tjson: %s" % tx_str)
# L2: convert string into binary data
tx_data = (tx_str + '\n').encode('ascii')
print("\tbinary: %r" % b2a_hex(tx_data))
# L1: tell operating system to send data
nbytes = conn.write(tx_data)
conn.flush()
# -------- SERVER PROCESSES MESSAGE --------
print("waiting...")
# L0: network stack waits for TCP response
# L1: operating system delivers binary data
rx_data = conn.readline()
print("\tbinary: %r" % b2a_hex(rx_data))
# L2: convert binary data into a string
rx_str = rx_data.decode('ascii')
print("\tjson: %s" % rx_str.rstrip())
# L3: convert string into Python object
rx_obj = json.loads(rx_str)
print("Received %s from server" % type(rx_obj))
# display result (is it correct?)
print("Max: %d | Min %d" % (rx_obj['max'], rx_obj['min']))
# close connection
conn.close()
import socket
import json
ADDRESS = ('127.0.0.1', 1234)
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect(ADDRESS)
conn = sock.makefile('rwb')
print("Connected to [%s:%s]" % (ADDRESS[0], ADDRESS[1]))
tx_obj = [8, 10, -12, 100, 2]
print(tx_obj)
tx_str = json.dumps(tx_obj)
tx_data = (tx_str + '\n').encode('ascii')
conn.write(tx_data)
conn.flush()
rx_data = conn.readline()
rx_str = rx_data.decode('ascii')
rx_obj = json.loads(rx_str)
print("Max: %d | Min %d" % (rx_obj['max'], rx_obj['min']))
conn.close()
import socket
import json
from binascii import b2a_hex
# create Socket object (IP, PORT)
ADDRESS = ('127.0.0.1', 1234)
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# start a server on IP:PORT combination
sock.bind(ADDRESS)
sock.setblocking(True)
sock.listen(1)
print("waiting...")
# wait for a client to connect
active_sock, address = sock.accept()
conn = active_sock.makefile('rwb')
print("Connection from [%s:%s]" % (address[0], address[1]))
# L0: network stack waits for TCP data
# L1: operating system delivers binary data
rx_data = conn.readline()
print("\tbinary %r" % b2a_hex(rx_data))
# L2: convert binary data into a string
rx_str = rx_data.decode('ascii')
print("\tjson: %s" % rx_str.rstrip())
# L3: convert string into Python data structure
rx_obj = json.loads(rx_str)
print("Received %s from client" % type(rx_obj))
tx_obj = {'max': max(rx_obj),
'min': min(rx_obj)}
input("Press [enter] to send response")
print("Sending %s to client" % type(tx_obj))
# L3: convert Python data structure into string
tx_str = json.dumps(tx_obj)
print("\tjson: %s" % tx_obj)
# L2: convert string into binary data
tx_data = (tx_str + '\n').encode('ascii')
print("\tbinary: %r" % b2a_hex(tx_data))
# L1: tell operating system to send data
conn.write(tx_data)
conn.flush()
# L0: network transmits TCP data
# close client connection
conn.close()
# shutdown server
sock.close()
import socket
import json
ADDRESS = ('127.0.0.1', 1234)
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.bind(ADDRESS)
sock.setblocking(True)
sock.listen(1)
active_sock, address = sock.accept()
conn = active_sock.makefile('rwb')
print("Connection from [%s:%s]" % (address[0], address[1]))
rx_data = conn.readline()
rx_str = rx_data.decode('ascii')
rx_obj = json.loads(rx_str)
tx_obj = {'max': max(rx_obj), 'min': min(rx_obj)}
tx_str = json.dumps(tx_obj)
tx_data = (tx_str + '\n').encode('ascii')
conn.write(tx_data)
conn.flush()
conn.close()
sock.close()
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment