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

python UDP CS demo的更多相关文章

  1. 【原创】NIO框架入门(二):服务端基于MINA2的UDP双向通信Demo演示

    前言 NIO框架的流行,使得开发大并发.高性能的互联网服务端成为可能.这其中最流行的无非就是MINA和Netty了,MINA目前的主要版本是MINA2.而Netty的主要版本是Netty3和Netty ...

  2. 【原创】NIO框架入门(一):服务端基于Netty4的UDP双向通信Demo演示

    申明:本文由作者基于日常实践整理,希望对初次接触MINA.Netty的人有所启发.如需与作者交流,见文签名,互相学习. 学习交流 更多学习资料:点此进入 推荐 移动端即时通讯交流: 215891622 ...

  3. Python UDP broadcast PermissionError: [Errno 13] Permission denied

    /********************************************************************** * Python UDP broadcast Permi ...

  4. RPi 2B python opencv camera demo example

    /************************************************************************************** * RPi 2B pyt ...

  5. Python Udp Socket

    socket(套接字),传输层通信的端点,由IP和端口号组成(IP,Port),可以通过socket精确地找到服务器上的进程并与之通信 python2.6实现,基于AF_INET(网络套接字) 类型S ...

  6. python udp编程实例

    与python tcp编程控制见 http://blog.csdn.net/aspnet_lyc/article/details/39854569 c++ udp/tcp 编程见 http://blo ...

  7. pyhanlp python 脚本的demo补充

    java demo https://github.com/hankcs/HanLP/tree/master/src/test/java/com/hankcs/demo github python de ...

  8. Python HTML Resolution Demo - SGMLParser & PyQuery

    1. SGMLParser: 这里定义了一个Parse类,继承SGMLParser里面的方法.使用一个变量is_h4做标记判定html文件中的h4标签,如果遇到h4标签,则将标签内的内容加入到Pars ...

  9. Python UDP小程序

    为了做UDP的测试,采用了nc和Python的服务器端. nc的安装和使用: yum install -y nc nc -vuz Python的UDP服务器端小程序: # -*- coding: UT ...

随机推荐

  1. Ubuntu 16.04 关闭/打开笔记本触摸板

    由于笔记本触摸板太多灵敏,影响使用,所以禁用掉触摸板. 禁用触摸板命令: sudo rmmod psmouse 启用触摸板命令 sudo modprobe psmouse 注意:启用之后可能会有几秒钟 ...

  2. php 模拟get和post提交方法[解决ajax跨域问题]

    get: $url = "http://www.111cn.net /index.php?a=b&c=d&e=f&g=" . urlencode('王璐个人 ...

  3. linux kernel的cmdline參数解析原理分析

    利用工作之便,今天研究了kernel下cmdline參数解析过程.记录在此.与大家共享.转载请注明出处.谢谢. Kernel 版本:3.4.55 Kernel启动时会解析cmdline,然后依据这些參 ...

  4. iOS_3_图片浏览

    终于效果图: BeyondViewController.h // // BeyondViewController.h // 03_图片浏览 // // Created by beyond on 14- ...

  5. js关于事件的一些总结(系列一)

    今天小弟在这里说一下 js 关于事件的一些总结  在这里直接上代码 省去啰嗦的步骤以免看烦了  总结的不好希望大家见谅 一.事件的默认事件 事件的默认事件是什么? 就是a标签有一个链接事件  inpu ...

  6. php单元测试标注(注解)

    @after 用于指明此方法应当在测试用例类中的每个测试方法运行完成之后调用. /** * @before1 */ public function bbb() { $this->assertTr ...

  7. drupal7 使用(hook_preprocess_HOOK)向各个主题模版里面传递变量

    函数地址:hook_preprocess_HOOK 1 首先解释下hook_preprocess_HOOK这个钩子的含义: hook           _     preprocess  _   H ...

  8. SDOI 2016 Round1 Day1

    储能表 /* 引自zyz大佬的数学思想 */ #include<cstdio> #include<iostream> using namespace std; typedef ...

  9. Java中线程和线程池

    Java中开启多线程的三种方式 1.通过继承Thread实现 public class ThreadDemo extends Thread{ public void run(){ System.out ...

  10. 智能家居DIY-空气质量检测篇-获取温度和湿度篇

    目录 智能家居DIY-空气质量检测篇-获取空气污染指数 前言 话说楼主终于升级当爸了,宝宝现在5个月了,宝宝出生的时候是冬天,正是魔都空气污染严重的时候,当时就想搞个自动开启空气净化器,由于种种原因一 ...