我正在尝试在2 RPI3,完全之间开始聊天,如图所示 https://www.raspberrypi.org/learning/networking-lessons/lesson-1/worksheet/
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)
答案 0 :(得分:1)
服务器文件:
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()