Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/AnonR/anonr.TX.../opt/imh-pyth.../lib/python2....
File: socket.py
# Wrapper module for _socket, providing some additional facilities
[0] Fix | Delete
# implemented in Python.
[1] Fix | Delete
[2] Fix | Delete
"""\
[3] Fix | Delete
This module provides socket operations and some related functions.
[4] Fix | Delete
On Unix, it supports IP (Internet Protocol) and Unix domain sockets.
[5] Fix | Delete
On other systems, it only supports IP. Functions specific for a
[6] Fix | Delete
socket are available as methods of the socket object.
[7] Fix | Delete
[8] Fix | Delete
Functions:
[9] Fix | Delete
[10] Fix | Delete
socket() -- create a new socket object
[11] Fix | Delete
socketpair() -- create a pair of new socket objects [*]
[12] Fix | Delete
fromfd() -- create a socket object from an open file descriptor [*]
[13] Fix | Delete
gethostname() -- return the current hostname
[14] Fix | Delete
gethostbyname() -- map a hostname to its IP number
[15] Fix | Delete
gethostbyaddr() -- map an IP number or hostname to DNS info
[16] Fix | Delete
getservbyname() -- map a service name and a protocol name to a port number
[17] Fix | Delete
getprotobyname() -- map a protocol name (e.g. 'tcp') to a number
[18] Fix | Delete
ntohs(), ntohl() -- convert 16, 32 bit int from network to host byte order
[19] Fix | Delete
htons(), htonl() -- convert 16, 32 bit int from host to network byte order
[20] Fix | Delete
inet_aton() -- convert IP addr string (123.45.67.89) to 32-bit packed format
[21] Fix | Delete
inet_ntoa() -- convert 32-bit packed format IP to string (123.45.67.89)
[22] Fix | Delete
ssl() -- secure socket layer support (only available if configured)
[23] Fix | Delete
socket.getdefaulttimeout() -- get the default timeout value
[24] Fix | Delete
socket.setdefaulttimeout() -- set the default timeout value
[25] Fix | Delete
create_connection() -- connects to an address, with an optional timeout and
[26] Fix | Delete
optional source address.
[27] Fix | Delete
[28] Fix | Delete
[*] not available on all platforms!
[29] Fix | Delete
[30] Fix | Delete
Special objects:
[31] Fix | Delete
[32] Fix | Delete
SocketType -- type object for socket objects
[33] Fix | Delete
error -- exception raised for I/O errors
[34] Fix | Delete
has_ipv6 -- boolean value indicating if IPv6 is supported
[35] Fix | Delete
[36] Fix | Delete
Integer constants:
[37] Fix | Delete
[38] Fix | Delete
AF_INET, AF_UNIX -- socket domains (first argument to socket() call)
[39] Fix | Delete
SOCK_STREAM, SOCK_DGRAM, SOCK_RAW -- socket types (second argument)
[40] Fix | Delete
[41] Fix | Delete
Many other constants may be defined; these may be used in calls to
[42] Fix | Delete
the setsockopt() and getsockopt() methods.
[43] Fix | Delete
"""
[44] Fix | Delete
[45] Fix | Delete
import _socket
[46] Fix | Delete
from _socket import *
[47] Fix | Delete
from functools import partial
[48] Fix | Delete
from types import MethodType
[49] Fix | Delete
[50] Fix | Delete
try:
[51] Fix | Delete
import _ssl
[52] Fix | Delete
except ImportError:
[53] Fix | Delete
# no SSL support
[54] Fix | Delete
pass
[55] Fix | Delete
else:
[56] Fix | Delete
def ssl(sock, keyfile=None, certfile=None):
[57] Fix | Delete
# we do an internal import here because the ssl
[58] Fix | Delete
# module imports the socket module
[59] Fix | Delete
import ssl as _realssl
[60] Fix | Delete
warnings.warn("socket.ssl() is deprecated. Use ssl.wrap_socket() instead.",
[61] Fix | Delete
DeprecationWarning, stacklevel=2)
[62] Fix | Delete
return _realssl.sslwrap_simple(sock, keyfile, certfile)
[63] Fix | Delete
[64] Fix | Delete
# we need to import the same constants we used to...
[65] Fix | Delete
from _ssl import SSLError as sslerror
[66] Fix | Delete
from _ssl import \
[67] Fix | Delete
RAND_add, \
[68] Fix | Delete
RAND_status, \
[69] Fix | Delete
SSL_ERROR_ZERO_RETURN, \
[70] Fix | Delete
SSL_ERROR_WANT_READ, \
[71] Fix | Delete
SSL_ERROR_WANT_WRITE, \
[72] Fix | Delete
SSL_ERROR_WANT_X509_LOOKUP, \
[73] Fix | Delete
SSL_ERROR_SYSCALL, \
[74] Fix | Delete
SSL_ERROR_SSL, \
[75] Fix | Delete
SSL_ERROR_WANT_CONNECT, \
[76] Fix | Delete
SSL_ERROR_EOF, \
[77] Fix | Delete
SSL_ERROR_INVALID_ERROR_CODE
[78] Fix | Delete
try:
[79] Fix | Delete
from _ssl import RAND_egd
[80] Fix | Delete
except ImportError:
[81] Fix | Delete
# LibreSSL does not provide RAND_egd
[82] Fix | Delete
pass
[83] Fix | Delete
[84] Fix | Delete
import os, sys, warnings
[85] Fix | Delete
[86] Fix | Delete
try:
[87] Fix | Delete
from cStringIO import StringIO
[88] Fix | Delete
except ImportError:
[89] Fix | Delete
from StringIO import StringIO
[90] Fix | Delete
[91] Fix | Delete
try:
[92] Fix | Delete
import errno
[93] Fix | Delete
except ImportError:
[94] Fix | Delete
errno = None
[95] Fix | Delete
EBADF = getattr(errno, 'EBADF', 9)
[96] Fix | Delete
EINTR = getattr(errno, 'EINTR', 4)
[97] Fix | Delete
[98] Fix | Delete
__all__ = ["getfqdn", "create_connection"]
[99] Fix | Delete
__all__.extend(os._get_exports_list(_socket))
[100] Fix | Delete
[101] Fix | Delete
[102] Fix | Delete
_realsocket = socket
[103] Fix | Delete
[104] Fix | Delete
# WSA error codes
[105] Fix | Delete
if sys.platform.lower().startswith("win"):
[106] Fix | Delete
errorTab = {}
[107] Fix | Delete
errorTab[10004] = "The operation was interrupted."
[108] Fix | Delete
errorTab[10009] = "A bad file handle was passed."
[109] Fix | Delete
errorTab[10013] = "Permission denied."
[110] Fix | Delete
errorTab[10014] = "A fault occurred on the network??" # WSAEFAULT
[111] Fix | Delete
errorTab[10022] = "An invalid operation was attempted."
[112] Fix | Delete
errorTab[10035] = "The socket operation would block"
[113] Fix | Delete
errorTab[10036] = "A blocking operation is already in progress."
[114] Fix | Delete
errorTab[10048] = "The network address is in use."
[115] Fix | Delete
errorTab[10054] = "The connection has been reset."
[116] Fix | Delete
errorTab[10058] = "The network has been shut down."
[117] Fix | Delete
errorTab[10060] = "The operation timed out."
[118] Fix | Delete
errorTab[10061] = "Connection refused."
[119] Fix | Delete
errorTab[10063] = "The name is too long."
[120] Fix | Delete
errorTab[10064] = "The host is down."
[121] Fix | Delete
errorTab[10065] = "The host is unreachable."
[122] Fix | Delete
__all__.append("errorTab")
[123] Fix | Delete
[124] Fix | Delete
[125] Fix | Delete
[126] Fix | Delete
def getfqdn(name=''):
[127] Fix | Delete
"""Get fully qualified domain name from name.
[128] Fix | Delete
[129] Fix | Delete
An empty argument is interpreted as meaning the local host.
[130] Fix | Delete
[131] Fix | Delete
First the hostname returned by gethostbyaddr() is checked, then
[132] Fix | Delete
possibly existing aliases. In case no FQDN is available, hostname
[133] Fix | Delete
from gethostname() is returned.
[134] Fix | Delete
"""
[135] Fix | Delete
name = name.strip()
[136] Fix | Delete
if not name or name == '0.0.0.0':
[137] Fix | Delete
name = gethostname()
[138] Fix | Delete
try:
[139] Fix | Delete
hostname, aliases, ipaddrs = gethostbyaddr(name)
[140] Fix | Delete
except error:
[141] Fix | Delete
pass
[142] Fix | Delete
else:
[143] Fix | Delete
aliases.insert(0, hostname)
[144] Fix | Delete
for name in aliases:
[145] Fix | Delete
if '.' in name:
[146] Fix | Delete
break
[147] Fix | Delete
else:
[148] Fix | Delete
name = hostname
[149] Fix | Delete
return name
[150] Fix | Delete
[151] Fix | Delete
[152] Fix | Delete
_socketmethods = (
[153] Fix | Delete
'bind', 'connect', 'connect_ex', 'fileno', 'listen',
[154] Fix | Delete
'getpeername', 'getsockname', 'getsockopt', 'setsockopt',
[155] Fix | Delete
'sendall', 'setblocking',
[156] Fix | Delete
'settimeout', 'gettimeout', 'shutdown')
[157] Fix | Delete
[158] Fix | Delete
if os.name == "nt":
[159] Fix | Delete
_socketmethods = _socketmethods + ('ioctl',)
[160] Fix | Delete
[161] Fix | Delete
if sys.platform == "riscos":
[162] Fix | Delete
_socketmethods = _socketmethods + ('sleeptaskw',)
[163] Fix | Delete
[164] Fix | Delete
# All the method names that must be delegated to either the real socket
[165] Fix | Delete
# object or the _closedsocket object.
[166] Fix | Delete
_delegate_methods = ("recv", "recvfrom", "recv_into", "recvfrom_into",
[167] Fix | Delete
"send", "sendto")
[168] Fix | Delete
[169] Fix | Delete
class _closedsocket(object):
[170] Fix | Delete
__slots__ = []
[171] Fix | Delete
def _dummy(*args):
[172] Fix | Delete
raise error(EBADF, 'Bad file descriptor')
[173] Fix | Delete
# All _delegate_methods must also be initialized here.
[174] Fix | Delete
send = recv = recv_into = sendto = recvfrom = recvfrom_into = _dummy
[175] Fix | Delete
__getattr__ = _dummy
[176] Fix | Delete
[177] Fix | Delete
# Wrapper around platform socket objects. This implements
[178] Fix | Delete
# a platform-independent dup() functionality. The
[179] Fix | Delete
# implementation currently relies on reference counting
[180] Fix | Delete
# to close the underlying socket object.
[181] Fix | Delete
class _socketobject(object):
[182] Fix | Delete
[183] Fix | Delete
__doc__ = _realsocket.__doc__
[184] Fix | Delete
[185] Fix | Delete
__slots__ = ["_sock", "__weakref__"] + list(_delegate_methods)
[186] Fix | Delete
[187] Fix | Delete
def __init__(self, family=AF_INET, type=SOCK_STREAM, proto=0, _sock=None):
[188] Fix | Delete
if _sock is None:
[189] Fix | Delete
_sock = _realsocket(family, type, proto)
[190] Fix | Delete
self._sock = _sock
[191] Fix | Delete
for method in _delegate_methods:
[192] Fix | Delete
setattr(self, method, getattr(_sock, method))
[193] Fix | Delete
[194] Fix | Delete
def close(self, _closedsocket=_closedsocket,
[195] Fix | Delete
_delegate_methods=_delegate_methods, setattr=setattr):
[196] Fix | Delete
# This function should not reference any globals. See issue #808164.
[197] Fix | Delete
self._sock = _closedsocket()
[198] Fix | Delete
dummy = self._sock._dummy
[199] Fix | Delete
for method in _delegate_methods:
[200] Fix | Delete
setattr(self, method, dummy)
[201] Fix | Delete
close.__doc__ = _realsocket.close.__doc__
[202] Fix | Delete
[203] Fix | Delete
def accept(self):
[204] Fix | Delete
sock, addr = self._sock.accept()
[205] Fix | Delete
return _socketobject(_sock=sock), addr
[206] Fix | Delete
accept.__doc__ = _realsocket.accept.__doc__
[207] Fix | Delete
[208] Fix | Delete
def dup(self):
[209] Fix | Delete
"""dup() -> socket object
[210] Fix | Delete
[211] Fix | Delete
Return a new socket object connected to the same system resource."""
[212] Fix | Delete
return _socketobject(_sock=self._sock)
[213] Fix | Delete
[214] Fix | Delete
def makefile(self, mode='r', bufsize=-1):
[215] Fix | Delete
"""makefile([mode[, bufsize]]) -> file object
[216] Fix | Delete
[217] Fix | Delete
Return a regular file object corresponding to the socket. The mode
[218] Fix | Delete
and bufsize arguments are as for the built-in open() function."""
[219] Fix | Delete
return _fileobject(self._sock, mode, bufsize)
[220] Fix | Delete
[221] Fix | Delete
family = property(lambda self: self._sock.family, doc="the socket family")
[222] Fix | Delete
type = property(lambda self: self._sock.type, doc="the socket type")
[223] Fix | Delete
proto = property(lambda self: self._sock.proto, doc="the socket protocol")
[224] Fix | Delete
[225] Fix | Delete
def meth(name,self,*args):
[226] Fix | Delete
return getattr(self._sock,name)(*args)
[227] Fix | Delete
[228] Fix | Delete
for _m in _socketmethods:
[229] Fix | Delete
p = partial(meth,_m)
[230] Fix | Delete
p.__name__ = _m
[231] Fix | Delete
p.__doc__ = getattr(_realsocket,_m).__doc__
[232] Fix | Delete
m = MethodType(p,None,_socketobject)
[233] Fix | Delete
setattr(_socketobject,_m,m)
[234] Fix | Delete
[235] Fix | Delete
socket = SocketType = _socketobject
[236] Fix | Delete
[237] Fix | Delete
class _fileobject(object):
[238] Fix | Delete
"""Faux file object attached to a socket object."""
[239] Fix | Delete
[240] Fix | Delete
default_bufsize = 8192
[241] Fix | Delete
name = "<socket>"
[242] Fix | Delete
[243] Fix | Delete
__slots__ = ["mode", "bufsize", "softspace",
[244] Fix | Delete
# "closed" is a property, see below
[245] Fix | Delete
"_sock", "_rbufsize", "_wbufsize", "_rbuf", "_wbuf", "_wbuf_len",
[246] Fix | Delete
"_close"]
[247] Fix | Delete
[248] Fix | Delete
def __init__(self, sock, mode='rb', bufsize=-1, close=False):
[249] Fix | Delete
self._sock = sock
[250] Fix | Delete
self.mode = mode # Not actually used in this version
[251] Fix | Delete
if bufsize < 0:
[252] Fix | Delete
bufsize = self.default_bufsize
[253] Fix | Delete
self.bufsize = bufsize
[254] Fix | Delete
self.softspace = False
[255] Fix | Delete
# _rbufsize is the suggested recv buffer size. It is *strictly*
[256] Fix | Delete
# obeyed within readline() for recv calls. If it is larger than
[257] Fix | Delete
# default_bufsize it will be used for recv calls within read().
[258] Fix | Delete
if bufsize == 0:
[259] Fix | Delete
self._rbufsize = 1
[260] Fix | Delete
elif bufsize == 1:
[261] Fix | Delete
self._rbufsize = self.default_bufsize
[262] Fix | Delete
else:
[263] Fix | Delete
self._rbufsize = bufsize
[264] Fix | Delete
self._wbufsize = bufsize
[265] Fix | Delete
# We use StringIO for the read buffer to avoid holding a list
[266] Fix | Delete
# of variously sized string objects which have been known to
[267] Fix | Delete
# fragment the heap due to how they are malloc()ed and often
[268] Fix | Delete
# realloc()ed down much smaller than their original allocation.
[269] Fix | Delete
self._rbuf = StringIO()
[270] Fix | Delete
self._wbuf = [] # A list of strings
[271] Fix | Delete
self._wbuf_len = 0
[272] Fix | Delete
self._close = close
[273] Fix | Delete
[274] Fix | Delete
def _getclosed(self):
[275] Fix | Delete
return self._sock is None
[276] Fix | Delete
closed = property(_getclosed, doc="True if the file is closed")
[277] Fix | Delete
[278] Fix | Delete
def close(self):
[279] Fix | Delete
try:
[280] Fix | Delete
if self._sock:
[281] Fix | Delete
self.flush()
[282] Fix | Delete
finally:
[283] Fix | Delete
if self._close:
[284] Fix | Delete
self._sock.close()
[285] Fix | Delete
self._sock = None
[286] Fix | Delete
[287] Fix | Delete
def __del__(self):
[288] Fix | Delete
try:
[289] Fix | Delete
self.close()
[290] Fix | Delete
except:
[291] Fix | Delete
# close() may fail if __init__ didn't complete
[292] Fix | Delete
pass
[293] Fix | Delete
[294] Fix | Delete
def flush(self):
[295] Fix | Delete
if self._wbuf:
[296] Fix | Delete
data = "".join(self._wbuf)
[297] Fix | Delete
self._wbuf = []
[298] Fix | Delete
self._wbuf_len = 0
[299] Fix | Delete
buffer_size = max(self._rbufsize, self.default_bufsize)
[300] Fix | Delete
data_size = len(data)
[301] Fix | Delete
write_offset = 0
[302] Fix | Delete
view = memoryview(data)
[303] Fix | Delete
try:
[304] Fix | Delete
while write_offset < data_size:
[305] Fix | Delete
self._sock.sendall(view[write_offset:write_offset+buffer_size])
[306] Fix | Delete
write_offset += buffer_size
[307] Fix | Delete
finally:
[308] Fix | Delete
if write_offset < data_size:
[309] Fix | Delete
remainder = data[write_offset:]
[310] Fix | Delete
del view, data # explicit free
[311] Fix | Delete
self._wbuf.append(remainder)
[312] Fix | Delete
self._wbuf_len = len(remainder)
[313] Fix | Delete
[314] Fix | Delete
def fileno(self):
[315] Fix | Delete
return self._sock.fileno()
[316] Fix | Delete
[317] Fix | Delete
def write(self, data):
[318] Fix | Delete
data = str(data) # XXX Should really reject non-string non-buffers
[319] Fix | Delete
if not data:
[320] Fix | Delete
return
[321] Fix | Delete
self._wbuf.append(data)
[322] Fix | Delete
self._wbuf_len += len(data)
[323] Fix | Delete
if (self._wbufsize == 0 or
[324] Fix | Delete
(self._wbufsize == 1 and '\n' in data) or
[325] Fix | Delete
(self._wbufsize > 1 and self._wbuf_len >= self._wbufsize)):
[326] Fix | Delete
self.flush()
[327] Fix | Delete
[328] Fix | Delete
def writelines(self, list):
[329] Fix | Delete
# XXX We could do better here for very long lists
[330] Fix | Delete
# XXX Should really reject non-string non-buffers
[331] Fix | Delete
lines = filter(None, map(str, list))
[332] Fix | Delete
self._wbuf_len += sum(map(len, lines))
[333] Fix | Delete
self._wbuf.extend(lines)
[334] Fix | Delete
if (self._wbufsize <= 1 or
[335] Fix | Delete
self._wbuf_len >= self._wbufsize):
[336] Fix | Delete
self.flush()
[337] Fix | Delete
[338] Fix | Delete
def read(self, size=-1):
[339] Fix | Delete
# Use max, disallow tiny reads in a loop as they are very inefficient.
[340] Fix | Delete
# We never leave read() with any leftover data from a new recv() call
[341] Fix | Delete
# in our internal buffer.
[342] Fix | Delete
rbufsize = max(self._rbufsize, self.default_bufsize)
[343] Fix | Delete
# Our use of StringIO rather than lists of string objects returned by
[344] Fix | Delete
# recv() minimizes memory usage and fragmentation that occurs when
[345] Fix | Delete
# rbufsize is large compared to the typical return value of recv().
[346] Fix | Delete
buf = self._rbuf
[347] Fix | Delete
buf.seek(0, 2) # seek end
[348] Fix | Delete
if size < 0:
[349] Fix | Delete
# Read until EOF
[350] Fix | Delete
self._rbuf = StringIO() # reset _rbuf. we consume it via buf.
[351] Fix | Delete
while True:
[352] Fix | Delete
try:
[353] Fix | Delete
data = self._sock.recv(rbufsize)
[354] Fix | Delete
except error, e:
[355] Fix | Delete
if e.args[0] == EINTR:
[356] Fix | Delete
continue
[357] Fix | Delete
raise
[358] Fix | Delete
if not data:
[359] Fix | Delete
break
[360] Fix | Delete
buf.write(data)
[361] Fix | Delete
return buf.getvalue()
[362] Fix | Delete
else:
[363] Fix | Delete
# Read until size bytes or EOF seen, whichever comes first
[364] Fix | Delete
buf_len = buf.tell()
[365] Fix | Delete
if buf_len >= size:
[366] Fix | Delete
# Already have size bytes in our buffer? Extract and return.
[367] Fix | Delete
buf.seek(0)
[368] Fix | Delete
rv = buf.read(size)
[369] Fix | Delete
self._rbuf = StringIO()
[370] Fix | Delete
self._rbuf.write(buf.read())
[371] Fix | Delete
return rv
[372] Fix | Delete
[373] Fix | Delete
self._rbuf = StringIO() # reset _rbuf. we consume it via buf.
[374] Fix | Delete
while True:
[375] Fix | Delete
left = size - buf_len
[376] Fix | Delete
# recv() will malloc the amount of memory given as its
[377] Fix | Delete
# parameter even though it often returns much less data
[378] Fix | Delete
# than that. The returned data string is short lived
[379] Fix | Delete
# as we copy it into a StringIO and free it. This avoids
[380] Fix | Delete
# fragmentation issues on many platforms.
[381] Fix | Delete
try:
[382] Fix | Delete
data = self._sock.recv(left)
[383] Fix | Delete
except error, e:
[384] Fix | Delete
if e.args[0] == EINTR:
[385] Fix | Delete
continue
[386] Fix | Delete
raise
[387] Fix | Delete
if not data:
[388] Fix | Delete
break
[389] Fix | Delete
n = len(data)
[390] Fix | Delete
if n == size and not buf_len:
[391] Fix | Delete
# Shortcut. Avoid buffer data copies when:
[392] Fix | Delete
# - We have no data in our buffer.
[393] Fix | Delete
# AND
[394] Fix | Delete
# - Our call to recv returned exactly the
[395] Fix | Delete
# number of bytes we were asked to read.
[396] Fix | Delete
return data
[397] Fix | Delete
if n == left:
[398] Fix | Delete
buf.write(data)
[399] Fix | Delete
del data # explicit free
[400] Fix | Delete
break
[401] Fix | Delete
assert n <= left, "recv(%d) returned %d bytes" % (left, n)
[402] Fix | Delete
buf.write(data)
[403] Fix | Delete
buf_len += n
[404] Fix | Delete
del data # explicit free
[405] Fix | Delete
#assert buf_len == buf.tell()
[406] Fix | Delete
return buf.getvalue()
[407] Fix | Delete
[408] Fix | Delete
def readline(self, size=-1):
[409] Fix | Delete
buf = self._rbuf
[410] Fix | Delete
buf.seek(0, 2) # seek end
[411] Fix | Delete
if buf.tell() > 0:
[412] Fix | Delete
# check if we already have it in our buffer
[413] Fix | Delete
buf.seek(0)
[414] Fix | Delete
bline = buf.readline(size)
[415] Fix | Delete
if bline.endswith('\n') or len(bline) == size:
[416] Fix | Delete
self._rbuf = StringIO()
[417] Fix | Delete
self._rbuf.write(buf.read())
[418] Fix | Delete
return bline
[419] Fix | Delete
del bline
[420] Fix | Delete
if size < 0:
[421] Fix | Delete
# Read until \n or EOF, whichever comes first
[422] Fix | Delete
if self._rbufsize <= 1:
[423] Fix | Delete
# Speed up unbuffered case
[424] Fix | Delete
buf.seek(0)
[425] Fix | Delete
buffers = [buf.read()]
[426] Fix | Delete
self._rbuf = StringIO() # reset _rbuf. we consume it via buf.
[427] Fix | Delete
data = None
[428] Fix | Delete
recv = self._sock.recv
[429] Fix | Delete
while True:
[430] Fix | Delete
try:
[431] Fix | Delete
while data != "\n":
[432] Fix | Delete
data = recv(1)
[433] Fix | Delete
if not data:
[434] Fix | Delete
break
[435] Fix | Delete
buffers.append(data)
[436] Fix | Delete
except error, e:
[437] Fix | Delete
# The try..except to catch EINTR was moved outside the
[438] Fix | Delete
# recv loop to avoid the per byte overhead.
[439] Fix | Delete
if e.args[0] == EINTR:
[440] Fix | Delete
continue
[441] Fix | Delete
raise
[442] Fix | Delete
break
[443] Fix | Delete
return "".join(buffers)
[444] Fix | Delete
[445] Fix | Delete
buf.seek(0, 2) # seek end
[446] Fix | Delete
self._rbuf = StringIO() # reset _rbuf. we consume it via buf.
[447] Fix | Delete
while True:
[448] Fix | Delete
try:
[449] Fix | Delete
data = self._sock.recv(self._rbufsize)
[450] Fix | Delete
except error, e:
[451] Fix | Delete
if e.args[0] == EINTR:
[452] Fix | Delete
continue
[453] Fix | Delete
raise
[454] Fix | Delete
if not data:
[455] Fix | Delete
break
[456] Fix | Delete
nl = data.find('\n')
[457] Fix | Delete
if nl >= 0:
[458] Fix | Delete
nl += 1
[459] Fix | Delete
buf.write(data[:nl])
[460] Fix | Delete
self._rbuf.write(data[nl:])
[461] Fix | Delete
del data
[462] Fix | Delete
break
[463] Fix | Delete
buf.write(data)
[464] Fix | Delete
return buf.getvalue()
[465] Fix | Delete
else:
[466] Fix | Delete
# Read until size bytes or \n or EOF seen, whichever comes first
[467] Fix | Delete
buf.seek(0, 2) # seek end
[468] Fix | Delete
buf_len = buf.tell()
[469] Fix | Delete
if buf_len >= size:
[470] Fix | Delete
buf.seek(0)
[471] Fix | Delete
rv = buf.read(size)
[472] Fix | Delete
self._rbuf = StringIO()
[473] Fix | Delete
self._rbuf.write(buf.read())
[474] Fix | Delete
return rv
[475] Fix | Delete
self._rbuf = StringIO() # reset _rbuf. we consume it via buf.
[476] Fix | Delete
while True:
[477] Fix | Delete
try:
[478] Fix | Delete
data = self._sock.recv(self._rbufsize)
[479] Fix | Delete
except error, e:
[480] Fix | Delete
if e.args[0] == EINTR:
[481] Fix | Delete
continue
[482] Fix | Delete
raise
[483] Fix | Delete
if not data:
[484] Fix | Delete
break
[485] Fix | Delete
left = size - buf_len
[486] Fix | Delete
# did we just receive a newline?
[487] Fix | Delete
nl = data.find('\n', 0, left)
[488] Fix | Delete
if nl >= 0:
[489] Fix | Delete
nl += 1
[490] Fix | Delete
# save the excess data to _rbuf
[491] Fix | Delete
self._rbuf.write(data[nl:])
[492] Fix | Delete
if buf_len:
[493] Fix | Delete
buf.write(data[:nl])
[494] Fix | Delete
break
[495] Fix | Delete
else:
[496] Fix | Delete
# Shortcut. Avoid data copy through buf when returning
[497] Fix | Delete
# a substring of our first recv().
[498] Fix | Delete
return data[:nl]
[499] Fix | Delete
12
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function