mirror of https://github.com/python/cpython.git
added read/writelines, bufsize to makefile, gethostbyaddr
This commit is contained in:
parent
b9e39c8861
commit
edd6376e5e
|
@ -54,6 +54,15 @@ def gethostbyname(str):
|
||||||
return macdnr.AddrToStr(id)
|
return macdnr.AddrToStr(id)
|
||||||
|
|
||||||
|
|
||||||
|
def gethostbyaddr(str):
|
||||||
|
id = _ipaddress(str)
|
||||||
|
ptr = macdnr.AddrToName(id)
|
||||||
|
ptr.wait()
|
||||||
|
name = ptr.cname
|
||||||
|
if name[-1:] == '.': name = name[:-1]
|
||||||
|
names, addresses = [], [str]
|
||||||
|
return name, names, addresses
|
||||||
|
|
||||||
def gethostname():
|
def gethostname():
|
||||||
global _myname
|
global _myname
|
||||||
if _myname == None:
|
if _myname == None:
|
||||||
|
@ -164,8 +173,8 @@ def listen(self, backlog):
|
||||||
self.stream.PassiveOpen(self.port)
|
self.stream.PassiveOpen(self.port)
|
||||||
self.listening = 1
|
self.listening = 1
|
||||||
|
|
||||||
def makefile(self, rw = 'r'):
|
def makefile(self, rw = 'r', bs = 512):
|
||||||
return _socketfile(self, rw)
|
return _socketfile(self, rw, bs)
|
||||||
|
|
||||||
def recv(self, bufsize, flags=0):
|
def recv(self, bufsize, flags=0):
|
||||||
if flags:
|
if flags:
|
||||||
|
@ -206,14 +215,15 @@ def __init__(self):
|
||||||
|
|
||||||
class _socketfile:
|
class _socketfile:
|
||||||
|
|
||||||
def __init__(self, sock, rw):
|
def __init__(self, sock, rw, bs):
|
||||||
if rw not in ('r', 'w'): raise _myerror, "mode must be 'r' or 'w'"
|
if rw not in ('r', 'w'): raise _myerror, "mode must be 'r' or 'w'"
|
||||||
self.sock = sock
|
self.sock = sock
|
||||||
self.rw = rw
|
self.rw = rw
|
||||||
|
self.bs = bs
|
||||||
self.buf = ''
|
self.buf = ''
|
||||||
|
|
||||||
def read(self, length = 0):
|
def read(self, length = -1):
|
||||||
if length <= 0:
|
if length < 0:
|
||||||
length = 0x7fffffff
|
length = 0x7fffffff
|
||||||
while len(self.buf) < length:
|
while len(self.buf) < length:
|
||||||
new = self.sock.recv(0x7fffffff)
|
new = self.sock.recv(0x7fffffff)
|
||||||
|
@ -240,8 +250,16 @@ def readline(self):
|
||||||
self.buf = self.buf[i+1:]
|
self.buf = self.buf[i+1:]
|
||||||
return rv
|
return rv
|
||||||
|
|
||||||
|
def readlines(self):
|
||||||
|
list = []
|
||||||
|
line = self.readline()
|
||||||
|
while line:
|
||||||
|
list.append(line)
|
||||||
|
line = self.readline()
|
||||||
|
return list
|
||||||
|
|
||||||
def write(self, buf):
|
def write(self, buf):
|
||||||
BS = 512
|
BS = self.bs
|
||||||
if len(buf) >= BS:
|
if len(buf) >= BS:
|
||||||
self.flush()
|
self.flush()
|
||||||
self.sock.send(buf)
|
self.sock.send(buf)
|
||||||
|
@ -250,7 +268,11 @@ def write(self, buf):
|
||||||
self.buf = buf
|
self.buf = buf
|
||||||
else:
|
else:
|
||||||
self.buf = self.buf + buf
|
self.buf = self.buf + buf
|
||||||
|
|
||||||
|
def writelines(self, list):
|
||||||
|
for line in list:
|
||||||
|
self.write(line)
|
||||||
|
|
||||||
def flush(self):
|
def flush(self):
|
||||||
if self.buf and self.rw == 'w':
|
if self.buf and self.rw == 'w':
|
||||||
self.sock.send(self.buf)
|
self.sock.send(self.buf)
|
||||||
|
|
Loading…
Reference in New Issue