mirror of https://github.com/python/cpython.git
fix several cosmetic things, add buffering to makefile
This commit is contained in:
parent
09b39fad8c
commit
cd259d0b40
|
@ -1,33 +1,38 @@
|
||||||
#
|
"""socket.py for mac - Emulate socket module with mactcp and macdnr
|
||||||
# socket.py for mac - Emulate socket module with mactcp and macdnr
|
|
||||||
#
|
Currently only implements TCP sockets (AF_INET, SOCK_STREAM).
|
||||||
# Jack Jansen, CWI, November 1994
|
Esoteric things like socket options don't work,
|
||||||
#
|
but getpeername() and makefile() do work; everything used by ftplib works!
|
||||||
|
"""
|
||||||
|
|
||||||
|
# Jack Jansen, CWI, November 1994 (initial version)
|
||||||
|
# Guido van Rossum, CWI, March 1995 (bug fixes and lay-out)
|
||||||
|
|
||||||
|
|
||||||
import mactcp
|
import mactcp
|
||||||
import MACTCP
|
import MACTCP
|
||||||
import macdnr
|
import macdnr
|
||||||
import sys
|
|
||||||
|
|
||||||
#
|
|
||||||
# Constants
|
# Exported constants
|
||||||
#
|
|
||||||
_myerror = 'socket_wrapper.error'
|
_myerror = 'socket_wrapper.error'
|
||||||
error = (mactcp.error, macdnr.error, _myerror)
|
error = (mactcp.error, macdnr.error, _myerror)
|
||||||
|
|
||||||
SOCK_DGRAM=1
|
SOCK_DGRAM = 1
|
||||||
SOCK_STREAM=2
|
SOCK_STREAM = 2
|
||||||
|
|
||||||
|
AF_INET = 1
|
||||||
|
|
||||||
AF_INET=1
|
|
||||||
|
|
||||||
#
|
|
||||||
# Internal constants
|
# Internal constants
|
||||||
#
|
|
||||||
_BUFSIZE=4096 # Size of tcp/udp input buffer
|
|
||||||
_connectionClosing=-42 # XXXX
|
|
||||||
|
|
||||||
_myaddress=None
|
_BUFSIZE = 4096 # Size of TCP/UDP input buffer
|
||||||
_myname=None
|
|
||||||
_myaddrstr=None
|
_myaddress = None
|
||||||
|
_myname = None
|
||||||
|
_myaddrstr = None
|
||||||
|
|
||||||
|
|
||||||
def _myipaddress():
|
def _myipaddress():
|
||||||
global _myaddress
|
global _myaddress
|
||||||
|
@ -35,17 +40,20 @@ def _myipaddress():
|
||||||
_myaddress = mactcp.IPAddr()
|
_myaddress = mactcp.IPAddr()
|
||||||
return _myaddress
|
return _myaddress
|
||||||
|
|
||||||
|
|
||||||
def _ipaddress(str):
|
def _ipaddress(str):
|
||||||
if type(str) == type(1):
|
if type(str) == type(1):
|
||||||
return str # Already numeric
|
return str # Already numeric
|
||||||
ptr = macdnr.StrToAddr(str)
|
ptr = macdnr.StrToAddr(str)
|
||||||
ptr.wait()
|
ptr.wait()
|
||||||
return ptr.ip0
|
return ptr.ip0
|
||||||
|
|
||||||
|
|
||||||
def gethostbyname(str):
|
def gethostbyname(str):
|
||||||
id = _ipaddress(str)
|
id = _ipaddress(str)
|
||||||
return macdnr.AddrToStr(id)
|
return macdnr.AddrToStr(id)
|
||||||
|
|
||||||
|
|
||||||
def gethostname():
|
def gethostname():
|
||||||
global _myname
|
global _myname
|
||||||
if _myname == None:
|
if _myname == None:
|
||||||
|
@ -55,46 +63,53 @@ def gethostname():
|
||||||
_myname = ptr.cname
|
_myname = ptr.cname
|
||||||
return _myname
|
return _myname
|
||||||
|
|
||||||
|
|
||||||
def _gethostaddress():
|
def _gethostaddress():
|
||||||
global _myaddrstr
|
global _myaddrstr
|
||||||
if _myaddrstr == None:
|
if _myaddrstr == None:
|
||||||
id = _myipaddress()
|
id = _myipaddress()
|
||||||
_myaddrstr = macdnr.AddrToStr(id)
|
_myaddrstr = macdnr.AddrToStr(id)
|
||||||
return _myaddrstr
|
return _myaddrstr
|
||||||
|
|
||||||
|
|
||||||
def socket(family, type, *which):
|
def socket(family, type, *which):
|
||||||
if family <> AF_INET:
|
if family <> AF_INET:
|
||||||
raise my_error, 'Protocol family not supported'
|
raise my_error, 'Protocol family %d not supported' % type
|
||||||
if type == SOCK_DGRAM:
|
if type == SOCK_DGRAM:
|
||||||
return _udpsocket()
|
return _udpsocket()
|
||||||
elif type == SOCK_STREAM:
|
elif type == SOCK_STREAM:
|
||||||
return _tcpsocket()
|
return _tcpsocket()
|
||||||
raise my_error, 'Protocol type not supported'
|
raise my_error, 'Protocol type %d not supported' % type
|
||||||
|
|
||||||
|
|
||||||
def fromfd(*args):
|
def fromfd(*args):
|
||||||
raise my_error, 'Operation not supported on a mac'
|
raise my_error, 'Operation not supported on a mac'
|
||||||
|
|
||||||
|
|
||||||
class _socket:
|
class _socket:
|
||||||
def accept(self, *args):
|
def unsupported(self, *args):
|
||||||
raise my_error, 'Operation not supported on this socket'
|
raise my_error, 'Operation not supported on this socket'
|
||||||
|
|
||||||
bind = accept
|
accept = unsupported
|
||||||
close = accept
|
bind = unsupported
|
||||||
connect = accept
|
close = unsupported
|
||||||
fileno = accept
|
connect = unsupported
|
||||||
getpeername = accept
|
fileno = unsupported
|
||||||
getsockname = accept
|
getpeername = unsupported
|
||||||
getsockopt = accept
|
getsockname = unsupported
|
||||||
listen = accept
|
getsockopt = unsupported
|
||||||
recv = accept
|
listen = unsupported
|
||||||
recvfrom = accept
|
recv = unsupported
|
||||||
send = accept
|
recvfrom = unsupported
|
||||||
sendto = accept
|
send = unsupported
|
||||||
setblocking = accept
|
sendto = unsupported
|
||||||
setsockopt = accept
|
setblocking = unsupported
|
||||||
shutdown = accept
|
setsockopt = unsupported
|
||||||
|
shutdown = unsupported
|
||||||
|
|
||||||
|
|
||||||
class _tcpsocket(_socket):
|
class _tcpsocket(_socket):
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.stream = mactcp.TCPCreate(_BUFSIZE)
|
self.stream = mactcp.TCPCreate(_BUFSIZE)
|
||||||
##self.stream.asr = self.asr
|
##self.stream.asr = self.asr
|
||||||
|
@ -111,8 +126,14 @@ def accept(self):
|
||||||
self.stream.wait()
|
self.stream.wait()
|
||||||
self.accepted = 1
|
self.accepted = 1
|
||||||
return self, self.getsockname()
|
return self, self.getsockname()
|
||||||
|
|
||||||
def bind(self, host, port):
|
# bind has two ways of calling: s.bind(host, port) or s.bind((host, port));
|
||||||
|
# the latter is more proper but the former more common
|
||||||
|
def bind(self, a1, a2=None):
|
||||||
|
if a2 is None:
|
||||||
|
host, port = a1
|
||||||
|
else:
|
||||||
|
host, port = a1, a2
|
||||||
self.port = port
|
self.port = port
|
||||||
|
|
||||||
def close(self):
|
def close(self):
|
||||||
|
@ -120,8 +141,13 @@ def close(self):
|
||||||
self.accepted = 0
|
self.accepted = 0
|
||||||
return
|
return
|
||||||
self.stream.Abort()
|
self.stream.Abort()
|
||||||
|
|
||||||
def connect(self, host, port):
|
# connect has the same problem as bind (see above)
|
||||||
|
def connect(self, a1, a2=None):
|
||||||
|
if a2 is None:
|
||||||
|
host, port = a1
|
||||||
|
else:
|
||||||
|
host, port = a1, a2
|
||||||
self.stream.ActiveOpen(self.port, _ipaddress(host), port)
|
self.stream.ActiveOpen(self.port, _ipaddress(host), port)
|
||||||
|
|
||||||
def getsockname(self):
|
def getsockname(self):
|
||||||
|
@ -138,8 +164,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):
|
def makefile(self, rw = 'r'):
|
||||||
return _socketfile(self)
|
return _socketfile(self, rw)
|
||||||
|
|
||||||
def recv(self, bufsize, flags=0):
|
def recv(self, bufsize, flags=0):
|
||||||
if flags:
|
if flags:
|
||||||
|
@ -170,20 +196,24 @@ def bytes_readable(self):
|
||||||
def bytes_writeable(self):
|
def bytes_writeable(self):
|
||||||
st = self.stream.Status()
|
st = self.stream.Status()
|
||||||
return st.sendWindow - st.sendUnacked;
|
return st.sendWindow - st.sendUnacked;
|
||||||
|
|
||||||
|
|
||||||
class _udpsocket(_socket):
|
class _udpsocket(_socket):
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
class _socketfile:
|
class _socketfile:
|
||||||
def __init__(self, sock):
|
|
||||||
|
def __init__(self, sock, rw):
|
||||||
|
if rw not in ('r', 'w'): raise ValueError, "mode must be 'r' or 'w'"
|
||||||
self.sock = sock
|
self.sock = sock
|
||||||
|
self.rw = rw
|
||||||
self.buf = ''
|
self.buf = ''
|
||||||
|
|
||||||
def read(self, *arg):
|
def read(self, length = 0):
|
||||||
if arg:
|
if length <= 0:
|
||||||
length = arg
|
|
||||||
else:
|
|
||||||
length = 0x7fffffff
|
length = 0x7fffffff
|
||||||
while len(self.buf) < length:
|
while len(self.buf) < length:
|
||||||
new = self.sock.recv(0x7fffffff)
|
new = self.sock.recv(0x7fffffff)
|
||||||
|
@ -209,14 +239,29 @@ def readline(self):
|
||||||
rv = self.buf[:i+1]
|
rv = self.buf[:i+1]
|
||||||
self.buf = self.buf[i+1:]
|
self.buf = self.buf[i+1:]
|
||||||
return rv
|
return rv
|
||||||
|
|
||||||
def write(self, buf):
|
def write(self, buf):
|
||||||
self.sock.send(buf)
|
BS = 512
|
||||||
|
if len(buf) >= BS:
|
||||||
|
self.flush()
|
||||||
|
self.sock.send(buf)
|
||||||
|
elif len(buf) + len(self.buf) >= BS:
|
||||||
|
self.flush()
|
||||||
|
self.buf = buf
|
||||||
|
else:
|
||||||
|
self.buf = self.buf + buf
|
||||||
|
|
||||||
|
def flush(self):
|
||||||
|
if self.buf and self.rw == 'w':
|
||||||
|
self.sock.send(self.buf)
|
||||||
|
self.buf = ''
|
||||||
|
|
||||||
def close(self):
|
def close(self):
|
||||||
self.sock.close()
|
self.flush()
|
||||||
|
##self.sock.close()
|
||||||
del self.sock
|
del self.sock
|
||||||
|
|
||||||
|
|
||||||
def __test_tcp():
|
def __test_tcp():
|
||||||
s = socket(AF_INET, SOCK_STREAM)
|
s = socket(AF_INET, SOCK_STREAM)
|
||||||
s.connect('poseidon.cwi.nl', 13)
|
s.connect('poseidon.cwi.nl', 13)
|
||||||
|
@ -227,10 +272,10 @@ def __test_tcp():
|
||||||
print 'Unexpected extra data:', rv
|
print 'Unexpected extra data:', rv
|
||||||
s.close()
|
s.close()
|
||||||
|
|
||||||
|
|
||||||
def __test_udp():
|
def __test_udp():
|
||||||
s = socket(AF_INET, SOCK_DGRAM)
|
s = socket(AF_INET, SOCK_DGRAM)
|
||||||
print 'Sending data... (hello world)'
|
print 'Sending data... (hello world)'
|
||||||
s.sendto(('poseidon.cwi.nl', 7), 'hello world')
|
s.sendto(('poseidon.cwi.nl', 7), 'hello world')
|
||||||
rv, host = s.recvfrom(1000)
|
rv, host = s.recvfrom(1000)
|
||||||
print 'Got from ', host, ':', rv
|
print 'Got from ', host, ':', rv
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue