python实现udp数据报传输的方法


Posted in Python onSeptember 26, 2014

本文实例讲述了Python实现UDP数据报传输的方法,非常具有实用价值。分享给大家供大家参考。具体方法分析如下:

服务端代码:

import socket 
port = 8081 
s = socket.socket(socket.AF_INET,socket.SOCK_DGRAM) 
#从给定的端口,从任何发送者,接收UDP数据报 
s.bind(("",port)) 
print 'waiting on port:',port 
while True: 
  data,addr = s.recvfrom(1024) 
  #接收一个数据报(最大到1024字节) 
  print 'reciveed:',data,"from",addr

客户端代码:

import socket 
port = 8081 
host = "localhost" 
s = socket.socket(socket.AF_INET,socket.SOCK_DGRAM) 
s.sendto("hello world",(host,port))

结果:先运行服务端,然后运行客户端,
服务端打印出:

waiting on port: 8081
reciveed: hello world from ('127.0.0.1', 62644)

补充:
socket.sendto(string[, flags], address)

官方文档如下:

Send data to the socket. The socket should not be connected to a remote socket, since the destination socket is specified by address. The optional flags argument has the same meaning as for recv() above. Return the number of bytes sent. (The format of address depends on the address family — see above.)address参数在协议类型为socket.SOCK_DGRAM时,address的结构为一个元组,(host,port)的格式

希望本文所述对大家的Python程序设计有所帮助。

Python 相关文章推荐
零基础写python爬虫之爬虫编写全记录
Nov 06 Python
Python THREADING模块中的JOIN()方法深入理解
Feb 18 Python
python使用PythonMagick将jpg图片转换成ico图片的方法
Mar 26 Python
python写入xml文件的方法
May 08 Python
python中scikit-learn机器代码实例
Aug 05 Python
Django中提供的6种缓存方式详解
Aug 05 Python
Python序列对象与String类型内置方法详解
Oct 22 Python
基于Django统计博客文章阅读量
Oct 29 Python
Python稀疏矩阵及参数保存代码实现
Apr 18 Python
解决python脚本中error: unrecognized arguments: True错误
Apr 20 Python
Python基础进阶之海量表情包多线程爬虫功能的实现
Dec 17 Python
Python就将所有的英文单词首字母变成大写
Feb 12 Python
python使用Berkeley DB数据库实例
Sep 26 #Python
python实现通过shelve修改对象实例
Sep 26 #Python
python实现在pickling的时候压缩的方法
Sep 25 #Python
python使用cPickle模块序列化实例
Sep 25 #Python
python使用marshal模块序列化实例
Sep 25 #Python
python中类的一些方法分析
Sep 25 #Python
python实现获取序列中最小的几个元素
Sep 25 #Python
You might like
暴雪前总裁遗憾:没尽早追赶Dota 取消星际争霸幽灵
2020/03/08 星际争霸
微信公众平台天气预报功能开发
2014/07/06 PHP
php简单检测404页面的方法示例
2019/08/23 PHP
php设计模式之中介者模式分析【星际争霸游戏案例】
2020/03/23 PHP
Javascript+XMLHttpRequest+asp.net无刷新读取数据库数据
2009/08/09 Javascript
JSON 教程 json入门学习笔记
2020/09/22 Javascript
HTML Dom与Css控制方法
2010/10/25 Javascript
基于Jquery的将DropDownlist的选中值赋给label的实现代码
2011/05/06 Javascript
获取元素距离浏览器周边的位置的方法getBoundingClientRect
2013/04/17 Javascript
JavaScript学习笔记之内置对象
2015/01/22 Javascript
js实现每日自动换一张图片的方法
2015/05/04 Javascript
JavaScript中boolean类型之三种情景实例代码
2016/11/21 Javascript
工作中常用的js、jquery自定义扩展函数代码片段汇总
2016/12/22 Javascript
使用JavaScript为一张图片设置备选路径的方法
2017/01/04 Javascript
js仿iphone秒表功能 计算平均数
2017/01/11 Javascript
jQuery获取单选按钮radio选中值与去除所有radio选中状态的方法
2017/05/20 jQuery
详解babel升级到7.X采坑总结
2019/05/12 Javascript
使用node.JS中的url模块解析URL信息
2020/02/06 Javascript
vue 使用 vue-pdf 实现pdf在线预览的示例代码
2020/04/26 Javascript
[47:12]TFT vs Secret Supermajor小组赛C组 BO3 第三场 6.3
2018/06/04 DOTA
Python冲顶大会 快来答题!
2018/01/17 Python
详解Python 中sys.stdin.readline()的用法
2019/09/12 Python
浅析python内置模块collections
2019/11/15 Python
python print 格式化输出,动态指定长度的实现
2020/04/12 Python
opencv python 图片读取与显示图片窗口未响应问题的解决
2020/04/24 Python
python Django 反向访问器的外键冲突解决
2020/05/20 Python
opencv 图像腐蚀和图像膨胀的实现
2020/07/07 Python
python安装cx_Oracle和wxPython的方法
2020/09/14 Python
澳洲国民品牌乡村路折扣店:Country Road & Trenery Outlet
2018/04/19 全球购物
生产副总岗位职责
2013/11/28 职场文书
写给爸爸的道歉信
2014/01/15 职场文书
行政助理的岗位职责
2014/02/18 职场文书
求职意向书
2014/04/01 职场文书
开会迟到检讨书范文
2015/05/06 职场文书
golang 实用库gotable的具体使用
2021/07/01 Golang
基于docker安装zabbix的详细教程
2022/06/05 Servers