python在twisted中实现二进制数据
实现不使用原有的basic.LineReceiver作为协议基类,而改用protocol.Protocol。因为原来sendline使用了的结束符。
协议定义先简单点,id + msg length + msg,
使用pack和unpack进行封装和解析,参考http://docs.python.org/library/struct.html
import struct
buffer = struct.pack("5s", 'hello', )
buffer = struct.pack("HH" , 1, len(buffer)) + buffer // H 对应unsigned short,2个字节,16位
print repr(buffer)
print struct.unpack("H", buffer[:2])[0]
print struct.unpack("H", buffer[2:4])[0]
print repr(struct.unpack("5s", buffer[4:])[0])
相关文档:
ZoundryDocument
Python skin is known for its color variations and for its elasticity; it is
the warmest leather of the season and ideal for the manufacture of many luxury
goods. Sometimes natural patterns can be hidden when they're done in black, but
the finish here has a bit of a shine to it ......
昨天试了下用HTMLParser类来解析网页,可发现结果并不理想。不管怎么说,先写下过程,希望后来人能在此基础上解决我所遇到的问题。
写了2套解决方案,当然这2套只能对特定网站有效。我这里主要说明下对BBC主页www.bbc.co.uk和对网易www.163.com的解析。
对于BBC:
这套要简单得多,可能是该网页的编码比较标准吧
import ......
昨天试了下用HTMLParser类来解析网页,可发现结果并不理想。不管怎么说,先写下过程,希望后来人能在此基础上解决我所遇到的问题。
写了2套解决方案,当然这2套只能对特定网站有效。我这里主要说明下对BBC主页www.bbc.co.uk和对网易www.163.com的解析。
对于BBC:
这套要简单得多,可能是该网页的编码比较标准吧
import ......
$ 字符串的末尾
^ 字符串的开始
\b 字符的边界
前缀t 字符串中的反斜线(所有字符)不转义
? 可选地匹配(位于之前的)单个字符
() 改变优先级,作为一个整体,一个组
| 或者
(A|B) 精确匹配A或B中的一个
{n,m} 匹配(位于之前的字符)n到m次
VERBOSE ......
偶然需要用到这样一个函数,在Delphi中,有现成的函数可以调用!在python中,我找了半天都还没找到,最后自己写了一个函数如下:
def dayOfMonth(date):
if date.month == 12:
return 31
else:
return (date.replace(month=date.month+1, day=1) - datetime.timedelta(days=1)).day
......