gpt4 book ai didi

Python:如何使用 Ubuntu Mate 创建超过 2 RPi 的简单聊天

转载 作者:太空宇宙 更新时间:2023-11-03 15:00:42 24 4
gpt4 key购买 nike

我正在尝试在 2 RPI3 之间开始聊天,完全,如图所示 https://www.raspberrypi.org/learning/networking-lessons/lesson-1/worksheet/

  1. 两台设备都有唯一的 IP,并且能够成功相互 ping 通。
  2. 两台设备均成功运行 chat.py(一台作为服务器 chat.py,一台作为客户端 chat.py 192.168.0.2

但是在每个终端上输入的文本不会通过。

有什么想法吗?

这是 chat.py 代码:

import network
import sys

def heard(phrase):
print("them:" + phrase)

if (len(sys.argv) >= 2):
network.call(sys.argv[1], whenHearCall=heard)
else:
network.wait(whenHearCall=heard)

while network.isConnected():
#phrase = raw_input() #python2
phrase = input() # python3
print("me:" + phrase)
network.say(phrase)

最佳答案

服务器文件:

import socket

def Main():
host = "127.0.0.1"
port = 5000

mySocket = socket.socket()
mySocket.bind((host,port))
mySocket.listen(2)
conn, addr = mySocket.accept()
print ("Connection from: " + str(addr))
varz=["var1","var2","var3"]
while True:
data = conn.recv(1024).decode()
if data in varz[0] :
print("YES!")
print(data.split(' '))
if not data:
break
print ("from connected user: " + str(data))

data = str(data[0:2]).upper()
print ("sending: " + str(data))
conn.send(data.encode())

conn.close()

if __name__ == '__main__':
Main()

客户端文件:

import socket

def Main():
host = '127.0.0.1'
port = 0


mySocket = socket.socket()
mySocket.connect((host,5000))
user1=input("type usename:")
message = input("%s: "%user1)

while message != 'q':
mySocket.send(message.encode())
data = mySocket.recv(1024).decode()

print ('Received from server: ' + data)

message = input("%s: "%user1)

mySocket.close()

if __name__ == '__main__':
Main()

关于Python:如何使用 Ubuntu Mate 创建超过 2 RPi 的简单聊天,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45149743/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com