Python发送UDP数据包
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18743962/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Python send UDP packet
提问by user2059619
I am trying to write a program to send UDP packets, as in https://wiki.python.org/moin/UdpCommunicationThe code appears to be in Python 2:
我正在尝试编写一个程序来发送 UDP 数据包,如 https://wiki.python.org/moin/UdpCommunication代码似乎在 Python 2 中:
import socket
UDP_IP = "127.0.0.1"
UDP_PORT = 5005
MESSAGE = "Hello, World!"
print "UDP target IP:", UDP_IP
print "UDP target port:", UDP_PORT
print "message:", MESSAGE
sock = socket.socket(socket.AF_INET, # Internet
socket.SOCK_DGRAM) # UDP
sock.sendto(MESSAGE, (UDP_IP, UDP_PORT))
If I put parenthesis around the printed stuff, it just prints it on the screen.
如果我在打印的内容周围加上括号,它只会将其打印在屏幕上。
What do I need to do to make this work?
我需要做什么才能完成这项工作?
采纳答案by Manoj Pandey
With Python3x, you need to convert your string to raw bytes. You would have to encode the string as bytes. Over the network you need to send bytes and not characters. You are right that this would work for Python 2x since in Python 2x, socket.sendto on a socket takes a "plain" string and not bytes. Try this:
使用 Python3x,您需要将字符串转换为原始字节。您必须将字符串编码为字节。通过网络,您需要发送字节而不是字符。你是对的,这适用于 Python 2x,因为在 Python 2x 中,套接字上的 socket.sendto 需要一个“普通”字符串而不是字节。尝试这个:
print("UDP target IP:", UDP_IP)
print("UDP target port:", UDP_PORT)
print("message:", MESSAGE)
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) # UDP
sock.sendto(bytes(MESSAGE, "utf-8"), (UDP_IP, UDP_PORT))
回答by Bill Lynch
Your code works as is for me.I'm verifying this by using netcaton Linux.
你的代码对我来说是有效的。我正在通过在 Linux 上使用netcat来验证这一点。
Using netcat, I can do nc -ul 127.0.0.1 5005
which will listen for packets at:
使用 netcat,我可以nc -ul 127.0.0.1 5005
在以下位置侦听数据包:
- IP: 127.0.0.1
- Port: 5005
- Protocol: UDP
- IP:127.0.0.1
- 端口:5005
- 协议:UDP
That being said, here's the output that I see when I run your script, while having netcat running.
话虽如此,这是我在运行您的脚本时看到的输出,同时运行 netcat。
[9:34am][wlynch@watermelon ~] nc -ul 127.0.0.1 5005
Hello, World!
回答by Steve Barnes
If you are running python 3 then you need to change the print statements to print functions, i.e. put things in brackets () after print statements.
如果您运行的是python 3,那么您需要将print 语句更改为print 函数,即在print 语句之后将内容放在方括号() 中。
The only thing that you will see the above do is the prints unless you have something listening on 127.0.0.1 port 5005
as you are sending a packetnot receivingit - so you need to implement and start the other part of the example in another console window firstso it is waiting for the message.
你会看到上面做的唯一的事情就是打印除非你有听上127.0.0.1 port 5005
为你发送一个数据包不接受它-所以你需要在另一个控制台窗口来实现和启动的例子的另一部分第一所以它是等待消息。
回答by pdp
Here is a complete example that has been tested with Python 2.7.5 on CentOS 7.
这是一个在 CentOS 7 上用 Python 2.7.5 测试过的完整示例。
#!/usr/bin/python
import sys, socket
def main(args):
ip = args[1]
port = int(args[2])
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
file = 'sample.csv'
fp = open(file, 'r')
for line in fp:
sock.sendto(line.encode('utf-8'), (ip, port))
fp.close()
main(sys.argv)
The program reads a file, sample.csv
from the current directory and sends each line in a separate UDP packet. If the program it were saved in a file named send-udp
then one could run it by doing something like:
该程序sample.csv
从当前目录读取一个文件,并在单独的 UDP 数据包中发送每一行。如果程序保存在一个名为的文件中,send-udp
则可以通过执行以下操作来运行它:
$ python send-udp 192.168.1.2 30088