python发布服务,Python服务发现:在本地n上发布服务
在本地網(wǎng)絡(luò)上執(zhí)行服務(wù)通告/發(fā)現(xiàn)的一個(gè)簡單方法是廣播UDP包。
常數(shù):PORT = 50000
MAGIC = "fna349fn" #to make sure we don't confuse or get confused by other programs
公告:from time import sleep
from socket import socket, AF_INET, SOCK_DGRAM, SOL_SOCKET, SO_BROADCAST, gethostbyname, gethostname
s = socket(AF_INET, SOCK_DGRAM) #create UDP socket
s.bind(('', 0))
s.setsockopt(SOL_SOCKET, SO_BROADCAST, 1) #this is a broadcast socket
my_ip= gethostbyname(gethostname()) #get our IP. Be careful if you have multiple network interfaces or IPs
while 1:
data = MAGIC+my_ip
s.sendto(data, ('', PORT))
print "sent service announcement"
sleep(5)
發(fā)現(xiàn):from socket import socket, AF_INET, SOCK_DGRAM
s = socket(AF_INET, SOCK_DGRAM) #create UDP socket
s.bind(('', PORT))
while 1:
data, addr = s.recvfrom(1024) #wait for a packet
if data.startswith(MAGIC):
print "got service announcement from", data[len(MAGIC):]
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)總結(jié)
以上是生活随笔為你收集整理的python发布服务,Python服务发现:在本地n上发布服务的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 水印鲁棒性的分析matlab程序,图像数
- 下一篇: mysql 8.0远程连接_安装mysq