我正在研究机器之间的进程间通信。目前我有 3 台笔记本电脑通过集线器连接,没有互联网连接。
有没有什么办法可以调整此代码以在没有 Internet 的情况下使用这样的代码在机器之间发送和接收消息?
import socket
import sys
try:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
except socket.error:
print("Failed to create a socket")
print("Socket created")
host = ("www.google.com")
port = 80
try:
remote_ip = socket.gethostbyname(host)
except socket.gaierror:
print("Hostname could not be found, exiting finding socket")
sys.exit()
print("Ip adress of",host,"is",remote_ip)
s.connect((remote_ip, port))
print("Socket connected to",host,"on ip", remote_ip)
message = "GET / HTTP/1.1\r\n\r\n"
try:
s.send(message.encode("utf-8"))
except socket.error:
print("Failed")
sys.exit
print("Message send successful")
reply = s.recv(4096)
print(reply)
s.close()