资源简介
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-BDD100K大规模多样化驾驶视频数据
- Instant Pygame for Python Game Development How
- Biopython Tutorial
- Think Python 2nd
- 一个小小的表白程序(python)
- Python课堂笔记(高淇400集第一季)
- 二级考试python试题12套(包括选择题和
- pywin32_python3.6_64位
- python+ selenium教程
- PycURL(Windows7/Win32)Python2.7安装包 P
- 英文原版-Scientific Computing with Python
- 7.图像风格迁移 基于深度学习 pyt
- 基于Python的学生管理系统
- A Byte of Python(简明Python教程)(第
- Python实例174946
- Python 人脸识别
- Python 人事管理系统
- 基于python-flask的个人博客系统
- 计算机视觉应用开发流程
- python 调用sftp断点续传文件
- python socket游戏
- 基于Python爬虫爬取天气预报信息
- python函数编程和讲解
- Python开发的个人博客
- 基于python的三层神经网络模型搭建
- python实现自动操作windows应用
- python人脸识别(opencv)
- python 绘图(方形、线条、圆形)
- python疫情卡UN管控
- python 连连看小游戏源码
评论
共有 条评论