UweKI hat geschrieben: ↑Montag 14. Juni 2021, 10:20
Es soll einfache nur eine Kommunikation zwischen zwei Raspberry Pi's gestellt werden und das mittels TCP-Socket und mittels Wireshark später analysiert.
Z.B. wäre es gut, wenn ich ein Text ausgeben kann "Hello World".
Hallo UweKI,
Ich will die Kommentar der anderen nicht in Frage stellen, aber ich denke du suchst ein einfache Lösung auf TCP-Ebene.
Um ein einfache TCP Communication zwischen zwei Raspberry Pis aufzubauen hast du mindestens zwei Möglichkeiten in Python:
Einmal über das socket interface oder asyncio streams.
Zu den Sockets gibt es hier ein kleines client-server Beispiel:
https://docs.python.org/3/library/socket.html#example
Du kannst dir daraus zwei Python Scripte erstellen: server.py und client.py
Beachte, dass ich hier im client.py die IP-Addresse auf 127.0.0.1 gesetzt habe, da ich das auf meinem Rechner ausprobiert habe.
Du kannst auch server.py und client.py jeweils auf die Raspberry Pis kopieren. Dann must du natürlich die IP-Addresse entsprechend ändern.
Code: Alles auswählen
# Echo server program
import socket
HOST = '' # Symbolic name meaning all available interfaces
PORT = 50007 # Arbitrary non-privileged port
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.bind((HOST, PORT))
s.listen(1)
conn, addr = s.accept()
with conn:
print('Connected by', addr)
while True:
data = conn.recv(1024)
if not data: break
conn.sendall(data)
Code: Alles auswählen
# Echo client program
import socket
HOST = '127.0.0.1' # The remote host
PORT = 50007 # The same port as used by the server
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.connect((HOST, PORT))
s.sendall(b'Hello, world')
data = s.recv(1024)
print('Received', repr(data))
Aus Wireshark exportiert erhält man z.B. folgende Ausgabe:
Man sieht sehr schon wie der TCP-connection handshake [SYN] [SYN,ACK][ACK] durchgeführt wird.
In Wireshark würdest du natürlich auch das "Hello World" in den Datenbytes wiederfinden.
Code: Alles auswählen
"No.","Time","Source","Destination","Protocol","Length","Info"
"1","0.000000","127.0.0.1","127.0.0.1","TCP","56","64439 > 50007 [SYN] Seq=0 Win=65535 Len=0 MSS=65495 WS=256 SACK_PERM=1"
"2","0.000044","127.0.0.1","127.0.0.1","TCP","56","50007 > 64439 [SYN, ACK] Seq=0 Ack=1 Win=65535 Len=0 MSS=65495 WS=256 SACK_PERM=1"
"3","0.000063","127.0.0.1","127.0.0.1","TCP","44","64439 > 50007 [ACK] Seq=1 Ack=1 Win=2619648 Len=0"
"4","0.000121","127.0.0.1","127.0.0.1","TCP","56","64439 > 50007 [PSH, ACK] Seq=1 Ack=1 Win=2619648 Len=12"
"5","0.000131","127.0.0.1","127.0.0.1","TCP","44","50007 > 64439 [ACK] Seq=1 Ack=13 Win=2619648 Len=0"
"6","0.000560","127.0.0.1","127.0.0.1","TCP","56","50007 > 64439 [PSH, ACK] Seq=1 Ack=13 Win=2619648 Len=12"
"7","0.000595","127.0.0.1","127.0.0.1","TCP","44","64439 > 50007 [ACK] Seq=13 Ack=13 Win=2619648 Len=0"
"8","0.000624","127.0.0.1","127.0.0.1","TCP","44","64439 > 50007 [FIN, ACK] Seq=13 Ack=13 Win=2619648 Len=0"
"9","0.000659","127.0.0.1","127.0.0.1","TCP","44","50007 > 64439 [ACK] Seq=13 Ack=14 Win=2619648 Len=0"
"10","0.000681","127.0.0.1","127.0.0.1","TCP","44","50007 > 64439 [FIN, ACK] Seq=13 Ack=14 Win=2619648 Len=0"
"11","0.000695","127.0.0.1","127.0.0.1","TCP","44","64439 > 50007 [ACK] Seq=14 Ack=14 Win=2619648 Len=0"
Hier währe eine fehlerhafte Verbindung, bei der der Server nicht antwortet und der client letztendlich die Verbindung abbricht:
Code: Alles auswählen
"No.","Time","Source","Destination","Protocol","Length","Info"
"1","0.000000","127.0.0.1","127.0.0.1","TCP","56","56037 > 50007 [SYN] Seq=0 Win=65535 Len=0 MSS=65495 WS=256 SACK_PERM=1"
"2","0.000021","127.0.0.1","127.0.0.1","TCP","44","50007 > 56037 [RST, ACK] Seq=1 Ack=1 Win=0 Len=0"
"3","0.511519","127.0.0.1","127.0.0.1","TCP","56","[TCP Retransmission] 56037 > 50007 [SYN] Seq=0 Win=65535 Len=0 MSS=65495 WS=256 SACK_PERM=1"
"4","0.511543","127.0.0.1","127.0.0.1","TCP","44","50007 > 56037 [RST, ACK] Seq=1 Ack=1 Win=0 Len=0"
"5","1.024936","127.0.0.1","127.0.0.1","TCP","56","[TCP Retransmission] 56037 > 50007 [SYN] Seq=0 Win=65535 Len=0 MSS=65495 WS=256 SACK_PERM=1"
"6","1.024955","127.0.0.1","127.0.0.1","TCP","44","50007 > 56037 [RST, ACK] Seq=1 Ack=1 Win=0 Len=0"
"7","1.537955","127.0.0.1","127.0.0.1","TCP","56","[TCP Retransmission] 56037 > 50007 [SYN] Seq=0 Win=65535 Len=0 MSS=65495 WS=256 SACK_PERM=1"
"8","1.537975","127.0.0.1","127.0.0.1","TCP","44","50007 > 56037 [RST, ACK] Seq=1 Ack=1 Win=0 Len=0"
"9","2.041275","127.0.0.1","127.0.0.1","TCP","56","[TCP Retransmission] 56037 > 50007 [SYN] Seq=0 Win=65535 Len=0 MSS=65495 WS=256 SACK_PERM=1"
"10","2.041294","127.0.0.1","127.0.0.1","TCP","44","50007 > 56037 [RST, ACK] Seq=1 Ack=1 Win=0 Len=0"
Für asyncio streams findest du hier ein analoges client server Beispiel:
https://docs.python.org/3/library/async ... l#examples