Introduction

We can use python programs to start a server side and client side network communication service and allow them to transmit message  to each other under a Raspberry Pi. For example, we start two LXTerminal Sessions under a Raspberry Pi (or two difference Pi), and then run the following script.

In Server Side, run the following script:

import socket
comms_socket = socket.socket()
comms_socket.bind((‘localhost’, 50000))
comms_socket.listen(10)
connection, address = comms_socket.accept()
while True:
    print(connection.recv(4096).decode(“UTF-8”))
    send_data = input(“Reply: “)
    connection.send(bytes(send_data, “UTF-8”))

In Client Side, run the following script:

import socket
comms_socket = socket.socket()
comms_socket.connect((‘localhost’, 50000))
while True:
    send_data = input(“message: “)
    comms_socket.send(bytes(send_data, “UTF-8”))
    print(comms_socket.recv(4096).decode(“UTF-8”))

The testing result is shown as below screen dump for your reference:

network-communicate-pi

Leave a Reply

Your email address will not be published.

Enter Captcha Here : *

Reload Image