资源简介
Python UDP实现可靠传输 停等协议
代码片段和文件信息
import sys
import socket
import random
import Checksum
‘‘‘
This is the basic sender class. Your sender will extend this class and will
implement the start() method.
‘‘‘
class BasicSender(object):
def __init__(selfdestportfilenamedebug=False):
self.debug = debug
self.dest = dest
self.dport = port
self.sock = socket.socket(socket.AF_INET socket.SOCK_DGRAM)
self.sock.settimeout(None) # blocking
self.sock.bind((‘‘random.randint(1000040000)))
if filename == None:
self.infile = sys.stdin
else:
self.infile = open(filename“rb“)
# Waits until packet is received to return.
def receive(self timeout=None):
self.sock.settimeout(timeout)
try:
return self.sock.recv(4096)
except (socket.timeout socket.error):
return None
# Sends a packet to the destination address.
def send(self message address=None):
if address is None:
address = (self.destself.dport)
self.sock.sendto(message address)
# Prepares a packet
def make_packet(selfmsg_typeseqnomsg):
body = “%s|%d|%s|“ % (msg_typeseqnomsg)
checksum = Checksum.generate_checksum(body)
packet = “%s%s“ % (bodychecksum)
return packet
def split_packet(self message):
pieces = message.split(‘|‘)
msg_type seqno = pieces[0:2] # first two elements always treated as msg type and seqno
checksum = pieces[-1] # last is always treated as checksum
data = ‘|‘.join(pieces[2:-1]) # everything in between is considered data
return msg_type seqno data checksum
# Main sending loop.
def start(self):
raise NotImplementedError
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
目录 0 2014-03-16 20:58 可靠的Sender\
文件 1768 2014-03-16 15:00 可靠的Sender\BasicSender.py
文件 458 2014-03-06 16:31 可靠的Sender\Checksum.py
文件 50688 2014-03-06 16:31 可靠的Sender\dfsf.doc
文件 2839 2014-03-16 15:40 可靠的Sender\Sender.py
文件 93 2014-03-16 14:57 可靠的Sender\命令行.txt
- 上一篇:安装iverlog和gtkwave
- 下一篇:《Python程序设计》题库
相关资源
- python实现SGBM图像匹配算法
- python实现灰度直方图均衡化
- scrapy_qunar_one
- Python学习全系列教程永久可用
- python简明教程.chm
- 抽奖大转盘python的图形化界面
- 双边滤波器实验报告及代码python
- python +MYSQL+HTML实现21蛋糕网上商城
- Python-直播答题助手自动检测出题搜索
- OpenCV入门教程+OpenCV官方教程中文版
- Python 串口工具源码+.exe文件
- Python开发的全栈股票系统.zip
- Python操作Excel表格并将其中部分数据写
- python书籍 PDF
- 利用python绘制散点图
- python+labview+No1.vi
- 老男孩python项目实战
- python源码制作whl文件.rar
- python3.5可用的scipy
- PYTHON3 经典50案例.pptx
- 计算机科学导论-python.pdf
- python模拟鼠标点击屏幕
- windows鼠标自动点击py脚本
- 鱼c小甲鱼零基础学python全套课后题和
- Python 练习题100道
- Practical Programming 2nd Edition
- wxPython Application Development Cookbook
- python 3.6
- Python 3.5.2 中文文档 互联网唯一CHM版本
- python3.5.2.chm官方文档
评论
共有 条评论