python写的帧分割函数
前一阵在写一个自动测试程序时需要按照特定字符对收到的数据进行帧的分割,原有要求是有每帧前后各有一个同样的特殊字符,当时估计下时间还够,想将其扩展为一个较为通用功能,即每帧数据前有各有一个特殊字符表示开始和结束,但前后这两字符可以不同,也可以只有一个(在头在尾分割效果不同),同时学习了一阵python,感觉其处理这类问题还比较合适,完成了以下初步的代码,此段代码性能上还需优化:
def frame_read(fd,head,tail,timeout):
'return -- donot recv data return None,recv data but wrong format return [],else return data in tupe format, \
frame_len -- each frame cmd len,'
if fd is None or len <= 0:
print 'fd is None or len <= 0,'
return None
fd_type = check_obj_type(fd)
# print 'connect obj type is %s' % fd_type
if fd_type == 'invalid_type':
return None
cur_time = time.time()
data = ''
lines = []
received = False
head_found = False
head_len = len(head)
tail_len = len(tail)
temp = None
frame_len = 512
hex_str = ''
while 1:
if is_timeout(cur_time,timeout):
print 'wait data timeout.'
if not received:
return None
else:
 
相关文档:
为了选择一个合适的脚本语言学习,今天查了不少有关Perl,Python,Ruby,Javascript的东西,可是发现各大阵营的人都在吹捧自己喜欢的语言,不过最没有争议的应该是Javascript现阶段还不适合用来做独立开发,它的天下还是在web应用上。 我主要是想做数据挖掘算法的研究,应该会处理大量的文本。提到文本处理,相信大部分人 ......
Python中执行系统命令常见方法有两种:
两者均需 import os
(1) os.system
# 仅仅在一个子终端运行系统命令,而不能获取命令执行后的返回信息
system(command) -> exit_status
Execute the command (a string) in a subshell.
# 如果再命令行下执行,结果直接打印出来
>>> os. ......
url配置
我们在polls这个app下创建一个
helloworld.py
from django.http import HttpResponse
def index(request):
return HttpResponse("Hello, Django.")
修改 urls.py
from django.conf.urls.defaults import *
# Uncomment the next two lines to enable the admin:
# from django.contrib ......
解压django,然后到其目录下安装
前提是你安装好python.并将其配置到环境变量中,然后去django的压缩文修的下,执行以下倒命令
python setup.py install
1.创建project
首先我们打开cmd, 定位到希望新建工程的目录下, 任意目录均可. 然后键入如下命令:
django-admin.py startproject hello其中hello为新工程目录文件名 ......