📄 netstring.py
字号:
def readns(sock):
"""read a netstring from a socket."""
size = ""
while 1:
c = sock.recv(1)
if c == ":":
break
elif not c:
raise IOError, "short netstring read"
size = size + c
size = sz = int(size)
s = ""
while sz:
ss = sock.recv(sz)
if not ss:
raise IOError, "short netstring read"
s += ss
sz -= len(ss)
if len(s) != size:
raise IOError, "short netstring read"
if sock.recv(1) != ",":
raise IOError, "missing netstring terminator"
return s
def writens(sock, s):
"""write a netstring to a socket."""
s = encode(s)
while len(s):
l = sock.send(s)
s = s[l:]
def encode(s):
return "%d:%s," % (len(s), s)
def decode(s):
try:
if s[-1] != ",":
raise ValueError
p = s.index(":")
if len(s) != p + int(s[0:p]) + 2:
raise ValueError
l = int(s[0:p])
return s[p+1:-1]
except ValueError:
raise ValueError, "netstring format error: " + s
def freadns(f):
"""read a netstring from a file."""
size = ""
while 1:
c = f.read(1)
if c == ":":
break
elif not c:
raise IOError, "short netstring read"
size = size + c
size = sz = int(size)
s = ""
while sz:
ss = f.read(sz)
if not ss:
raise IOError, "short netstring read"
s += ss
sz -= len(ss)
if len(s) != size:
raise IOError, "short netstring read"
if f.read(1) != ",":
raise IOError, "missing netstring terminator"
return s
def fwritens(f, s):
"""write a netstring to a file."""
s = encode(s)
f.write(s)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -