55import pickle
66from io import BytesIO
77import sys
8- __all__ = ['PickleSocket' , 'RemoteClosedUnexpectedly ' ]
8+ __all__ = ['PickleSocket' , 'RemoteClosedDuringPickle ' ]
99
1010class PickleSocket ():
1111 def __init__ (self ,upon_this_socket = None ):
1212 if upon_this_socket is None :
1313 self .socket = socket .socket ()
1414 else :
1515 self .socket = upon_this_socket
16+ for name in dir (self .socket ):
17+ if name [:2 ] != '__' :
18+ self .__setattr__ (name , self .socket .__getattribute__ (name ))
1619
1720 def shakeHands (self ,banner = 'This is a pickleSocket by Daniel Chin. ' ,echo = True ):
1821 self .sendObj (banner )
@@ -27,29 +30,6 @@ def shakeHands(self,banner='This is a pickleSocket by Daniel Chin. ',echo=True):
2730 input ('Enter to exit...' )
2831 sys .exit (1 )
2932
30- def bind (self ,address ,local = False ):
31- '''
32- if `local` is True, address is no longer a tuple, but a port int.
33- '''
34- if local :
35- self .socket .bind (('127.0.0.1' ,address ))
36- else :
37- self .socket .bind (address )
38-
39- def listen (self ,capacity ):
40- self .socket .listen (capacity )
41-
42- def connect (self ,address ,AB = False ,dorm = False ,local = False ):
43- '''if AB or dorm is True, address is no longer a tuple, but a port int.'''
44- if AB :
45- self .socket .connect (('10.209.1.45' ,address ))
46- elif dorm :
47- self .socket .connect (('10.209.23.186' ,address ))
48- elif local :
49- self .socket .connect (('127.0.0.1' ,address ))
50- else :
51- self .socket .connect (address )
52-
5333 def sendObj (self ,obj ):
5434 io_obj = BytesIO ()
5535 pickle .dump (obj ,io_obj )
@@ -61,20 +41,6 @@ def sendObj(self,obj):
6141
6242 def recvObj (self ):
6343 return pickle .load (IoSocket (self .socket ))
64-
65- def send (self ,* args ):
66- return self .socket .send (* args )
67-
68- def recv (self ,* args ):
69- return self .socket .recv (* args )
70-
71- def accept (self ):
72- s ,addr = self .socket .accept ()
73- s = self .__class__ (s )
74- return s ,addr
75-
76- def close (self ):
77- return self .socket .close ()
7844
7945class IoSocket :
8046 def __init__ (self ,socket ):
@@ -83,8 +49,8 @@ def __init__(self,socket):
8349 def read (self ,count = 1 ):
8450 read = self .socket .recv (count )
8551 if read == b'' :
86- raise RemoteClosedUnexpectedly
87- return read
52+ raise RemoteClosedDuringPickle
53+ return read
8854
8955 def readline (self ):
9056 read = b''
@@ -94,5 +60,9 @@ def readline(self):
9460 buffer += read
9561 return buffer
9662
97- class RemoteClosedUnexpectedly (BaseException ):
63+ class RemoteClosedDuringPickle (BaseException ):
9864 pass
65+
66+ if __name__ == '__main__' :
67+ from console import console
68+ console (globals ())
0 commit comments