简体   繁体   English

如何使用 python 通过 Internet 连接广播消息?

[英]How do I broadcast a message over an internet connection using python?

I am currently looking for a way to broadcast a message to connected devices using a standard internet connection.我目前正在寻找一种使用标准互联网连接向连接设备广播消息的方法。 I have written some code but I am kind of confused as to where to add a script that sends a message to devices such as laptops, androids, and ios devices.我已经编写了一些代码,但是对于在哪里添加向笔记本电脑、机器人和 ios 设备等设备发送消息的脚本,我感到有点困惑。 My code is:我的代码是:

# Send UDP broadcast packets

MYPORT = 50000

import sys, time
from socket import *

s = socket(AF_INET, SOCK_DGRAM)
s.bind(('', 0))
s.setsockopt(SOL_SOCKET, SO_BROADCAST, 1)

while 1:
    data = repr(time.time()) + '\n'
    s.sendto(data, ('<broadcast>', MYPORT))
    time.sleep(2)

Just a little mistake in your code.只是代码中的一个小错误。 replace s.sendto(data, ('<broadcast>', MYPORT)) by s.sendto(data.encode(), ('<broadcast>', MYPORT)) and it works !用 s.sendto s.sendto(data.encode(), ('<broadcast>', MYPORT))替换s.sendto(data, ('<broadcast>', MYPORT)) , MYPORT)) 就可以了!

It's because sendto need a bytes-like object and not str这是因为 sendto 需要一个类似字节的 object 而不是 str

So again I'll iterate that I think its best you look into IPv4 and UDP and how they work.因此,我将再次重申,我认为最好查看 IPv4 和 UDP 以及它们的工作原理。 But I'll explain a little here and give some code.但我会在这里稍微解释一下并给出一些代码。

UDP has to bind to a port on a device in order to be able to listen for messages. UDP 必须绑定到设备上的端口才能监听消息。 If my pc has ip of 192.168.0.2 and I have a laptop at 192.168.0.3 and i want to use udp then I would need to do this.如果我的电脑有 192.168.0.2 的 ip 并且我在 192.168.0.3 有一台笔记本电脑并且我想使用 udp 那么我需要这样做。 First I would need to decide on a port to use.首先,我需要决定要使用的端口。 I'll use 50000.我会用50000。

PC (192.168.0.2)个人电脑 (192.168.0.2)

from socket import *

# Create socket that can broadcast
s = socket(AF_INET, SOCK_DGRAM)
s.setsockopt(SOL_SOCKET, SO_BROADCAST, 1)

# Bind to '' meaning we will listen for any message from any IP
# Bind to port 50000 so we can get messages sent to that port
s.bind(('', 50000))

# I'll broadcast a message to all devices on the network that are listening on port 50000
# But I could broadcast to any port. My listen port and send port don't have to be the same.
s.sendto(b"test", ('<broadcast>', 50000))

Laptop (192.168.0.3)笔记本电脑 (192.168.0.3)

from socket import *

# Create a socket that is bound to port 50000
s = socket(AF_INET, SOCK_DGRAM)
s.bind(('', 50000))

# Wait for a message to come in on port 50000, and print it
print(s.recvfrom(1024))

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

相关问题 如果失去互联网连接,如何重置python程序 - How do I reset a python program in the event of internet connection lost Python中通过互联网的套接字连接? - Socket connection over internet in Python? 在Windows中使用Python,如何通过套接字连接发送和检索任意数量的数据? - Using Python in Windows, how do I send and retrieve an arbitrary amount of data over a socket connection? ip python windows 10上的BroadCast消息 - BroadCast message over ip python windows 10 Python requests.get():如果出现 Internet 连接错误,如何打印消息? - Python requests.get(): How to Print a Message if There is an Internet Connection Error? 如何使用 JavaScript 和 Flask_socketio 向特定的 URL 广播消息? - How do I broadcast a message to a specific URL using JavaScript and Flask_socketio? 如何使用python套接字库在即席wifi上广播 - How to broadcast over adhoc wifi using the python sockets library 如何使用python-magic通过Internet获取文件的文件类型? - How do I use python-magic to get the file type of a file over the Internet? 如何在没有互联网连接的Windows XP计算机上安装Python psutil库? - How do I install the Python psutil library on a Windows XP machine without an internet connection? 如何在没有互联网连接的情况下在raspberry pi中安装python库? - How do I install python libraries in raspberry pi without an internet connection?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM