python UDP CS demo

简介:

UDP Communication

 

See also SoapOverUdpTcpCommunication

 

Sending

Here's simple code to post a note by UDP in Python:

 

Toggle line numbers
   1 import socket  2   3 UDP_IP = "127.0.0.1"  4 UDP_PORT = 5005  5 MESSAGE = "Hello, World!"  6   7 print "UDP target IP:", UDP_IP  8 print "UDP target port:", UDP_PORT  9 print "message:", MESSAGE  10   11 sock = socket.socket(socket.AF_INET, # Internet  12  socket.SOCK_DGRAM) # UDP  13 sock.sendto(MESSAGE, (UDP_IP, UDP_PORT)) 

 

Receiving

Here's simple code to receive UDP messages in Python:

 

Toggle line numbers
   1 import socket  2   3 UDP_IP = "127.0.0.1"  4 UDP_PORT = 5005  5   6 sock = socket.socket(socket.AF_INET, # Internet  7  socket.SOCK_DGRAM) # UDP  8 sock.bind((UDP_IP, UDP_PORT))  9   10 while True:  11  data, addr = sock.recvfrom(1024) # buffer size is 1024 bytes  12  print "received message:", data 

 

Using UDP for e.g. File Transfers

If considering extending this example for e.g. file transfers, keep in mind that UDP is not reliable. So you'll have to handle packets getting lost and packets arriving out of order. In effect, to get something reliable you'll need to implement something similar to TCP on top of UDP, and you might want to consider using TCP instead.

That being said, sometimes you need to use UDP, e.g. for UDP hole punching. In that case, consider TFTP for python or UDT for python










本文转自张昺华-sky博客园博客,原文链接:http://www.cnblogs.com/bonelee/p/6420218.html,如需转载请自行联系原作者


相关文章
|
29天前
|
存储 Python
Python网络编程基础(Socket编程) UDP 发送和接收数据
【4月更文挑战第10天】对于UDP客户端而言,发送数据是一个相对简单的过程。首先,你需要构建一个要发送的数据报,这通常是一个字节串(bytes)。然后,你可以调用socket对象的`sendto`方法,将数据报发送到指定的服务器地址和端口。
|
1月前
|
存储 Python
Python网络编程基础(Socket编程)UDP客户端编程
【4月更文挑战第9天】在UDP通信中,客户端负责发送数据到服务器,并接收来自服务器的响应。与服务器不同,客户端通常不需要绑定到特定的地址和端口,因为它可以临时使用任何可用的端口来发送数据。下面,我们将详细讲解UDP客户端编程的基本步骤。
|
1月前
|
网络协议 Python
Python网络编程基础(Socket编程)创建UDP socket对象
【4月更文挑战第8天】在Python中创建UDP服务器涉及使用`socket`模块创建socket对象,如`udp_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)`,然后绑定到特定IP地址和端口,如`udp_socket.bind(('localhost', 12345))`。服务器通过`recvfrom`在无限循环中监听和接收数据报。这只是基础,实际应用还需处理接收、解析、响应及错误处理等。接下来可学习如何利用socket对象进行数据交互以构建完整服务器。
|
1月前
|
网络协议 网络性能优化 开发者
Python网络编程基础(Socket编程)UDP Socket编程
【4月更文挑战第8天】Python网络编程中,UDP与TCP协议各有特点。TCP提供可靠连接,确保数据顺序与完整性,适合文件传输等;UDP则无连接,速度快,常用于实时音视频,牺牲了数据可靠性。Python的socket库支持两者,开发者可根据需求选择。
|
3月前
|
网络协议 Python
在Python中进行UDP(User Datagram Protocol)网络编程
在Python中进行UDP(User Datagram Protocol)网络编程
31 3
|
10天前
|
网络协议 算法 网络架构
Python网络编程之udp编程、黏包以及解决方案、tcpserver
Python网络编程之udp编程、黏包以及解决方案、tcpserver
|
15天前
|
存储 网络协议 关系型数据库
Python从入门到精通:2.3.2数据库操作与网络编程——学习socket编程,实现简单的TCP/UDP通信
Python从入门到精通:2.3.2数据库操作与网络编程——学习socket编程,实现简单的TCP/UDP通信
|
1月前
|
Python
Python网络编程基础(Socket编程)UDP服务器编程
【4月更文挑战第8天】Python UDP服务器编程使用socket库创建UDP套接字,绑定到特定地址(如localhost:8000),通过`recvfrom`接收客户端数据报,显示数据长度、地址和内容。无连接的UDP协议使得服务器无法主动发送数据,通常需应用层实现请求-响应机制。当完成时,用`close`关闭套接字。
|
1月前
|
Python
Python 循环使用demo
【4月更文挑战第3天】在Python中,主要的循环结构有for和while。示例包括:使用for循环打印列表[1, 2, 3, 4, 5],以及使用while循环计算1到10的和。`for i in [1, 2, 3, 4, 5]: print(i)`,以及`while count <= 10: sum += count; count += 1; print(sum)`。
12 2
|
2月前
|
网络协议 Python
Python网络编程实现TCP和UDP连接
Python网络编程实现TCP和UDP连接
33 0