简体   繁体   English

Python DUP广播发送器/接收器和广播IP地址功能

[英]Python DUP broadcast sender/receiver and broadcast IP address function

from broadcast sender 从广播发件人

import socket, traceback

host = ''                               # Bind to all interfaces
port = 51423
broadcastaddr=findbroadcastaddr();
addr=(broadcastaddr, port)

s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)                
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)   #broadcasr

data='hello from sender'

s.bind('', port)                  #socket binding to any host
s.sendto(data, addr):
s.close

1) This is a broadcast sender, I don't know the findbroadcastaddr function, basically if my network address is 192.1.3.0 then my broadcast address would be 192.1.3.255. 1)这是一个广播发送者,我不知道findbroadcastaddr函数,基本上,如果我的网络地址是192.1.3.0,那么我的广播地址将是192.1.3.255。 Anybody know that function. 任何人都知道该功能。

broadcast receiver i 广播接收器我

mport socket

# Set the socket parameters
addr = ('', 33333)  # host, port

# Create socket and bind to address
UDPSock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
UDPSock.bind(addr)

# Receive messages
while True:
    data, addr = UDPSock.recvfrom(1024)

2)1024 is the maximum number of data of bytes can be received. 2)1024是可以接收的最大字节数据数。 and the result from recvfrom breaks down into 2 fields ; 并且recvfrom的结果分为2个字段; the first part is data and second is where it comes from? 第一部分是数据,第二部分是数据的来源?

The following calculates a LAN broadcast address given the local machine's IP. 给定本地计算机的IP,以下内容将计算LAN广播地址。 It sets the 4th octet to 255, so host on 192.168.1.12 will get a LAN broadcast IP of 192.168.1.255 它设置了第四个八位到255,所以主机192.168.1.12会得到192.168.1.255的LAN IP广播

from socket import *

myip = gethostbyname(gethostname())
print 'My IP',myip

# XX: assumes /24 address
broadip = inet_ntoa( inet_aton(myip)[:3] + b'\xff' )
print 'LAN broadcast', broadip

I disagree with the answer provided by johntellsall , which merely "sets the 4th octet to 255". 我不同意johntellsall提供答案 ,该答案仅“将第四个八位位组设置为255”。

You intend to calculate the broadcast address correctly, it depends on your netmask and not only your IP address. 您打算正确计算广播地址,它取决于您的网络掩码 ,而不仅取决于IP地址。 In some cases it may come down to setting the 4th octet to 0xFF . 某些情况下,它可能归结为将第四个八位位组设置为0xFF

The proper steps to calculate a broadcast address are: 计算广播地址的正确步骤是:

  1. binary negate the netmask 二进制否定网络掩码
  2. take the result and then OR it with the host IP address 拍摄效果,然后将其与主机IP地址

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python无法使用以下命令发送广播包 <broadcast> 地址 - Python can't send a broadcast package with <broadcast> address ip python windows 10上的BroadCast消息 - BroadCast message over ip python windows 10 Python UDP 广播地址异常 Windows 10 - Python UDP broadcast address anomaly on Windows 10 如何在本地主机中使用 python 套接字获取广播地址? - How to get broadcast address with python socket in localhost? Python:在Windows上获取UDP广播/ socket.recvcmsg()的发送者 - Python: Get sender of UDP broadcast / socket.recvcmsg() on Windows 向所有连接的客户端广播,除了使用python flask socketio的发件人 - Broadcast to all connected clients except sender with python flask socketio 从 ip 和掩码获取广播地址和网络地址 - Get broadcast adrdess and network address from an ip and mask 为什么将pdst与目标ip地址绑定并绑定广播的mac地址而不是目标mac地址 - Why binding pdst with the target ip address and binding a broadcast mac address instead of the target mac address Android广播接收器无法正常工作:绑定失败,无法分配请求地址 - Android broadcast receiver doesn't work: the binding failed and is unable to assign request address 如何在Python中找到局域网的广播地址? - How can I find the broadcast address of a LAN in Python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM