[vQ@sdZddlZddlZddlZddlZddlZddlZddlZddlZddl Z ddl Z ddl Z ddl Z ddl Z ddlZddlZddZddZdddZGd d d ejZd d Zd ZGdddejZiZe jdZe jdZGdddeZGdddeZddZ GdddeZ!Gdddej"eZ#GdddeZ$GdddeZ%ddZ&d d!Z'Gd"d#d#eZ(d$d%Z)dS)&aRPC Implemention, originally written for the Python Idle IDE For security reasons, GvR requested that Idle's Python execution server process connect to the Idle process, which listens for the connection. Since Idle has only one client per server, this was not a limitation. +---------------------------------+ +-------------+ | socketserver.BaseRequestHandler | | SocketIO | +---------------------------------+ +-------------+ ^ | register() | | | unregister()| | +-------------+ | ^ ^ | | | | + -------------------+ | | | | +-------------------------+ +-----------------+ | RPCHandler | | RPCClient | | [attribute of RPCServer]| | | +-------------------------+ +-----------------+ The RPCServer handler class is expected to provide register/unregister methods. RPCHandler inherits the mix-in class SocketIO, which provides these methods. See the Idle run.main() docstring for further information on how this was accomplished in Idle. NcCs+tj|}t|tjs't|S)N)marshalloads isinstancetypesCodeTypeAssertionError)mscor !/usr/lib/python3.4/idlelib/rpc.py unpickle_code/sr cCs4t|tjsttj|}t|ffS)N)rrrrrdumpsr )r rr r r pickle_code4srcCs2tj}t||}|j||jS)N)ioBytesIO CodePicklerdumpgetvalue)objZprotocolfpr r r r 9s  r c@s0eZdZieej6ZejejdS)rN) __name__ __module__ __qualname__rrrdispatch_tableupdatecopyregr r r r r?s riz 127.0.0.1c@sOeZdZdddZddZddZdd Zd d ZdS) RPCServerNcCs/|dkrt}ntjj|||dS)N) RPCHandler socketserver TCPServer__init__)selfaddrZ handlerclassr r r r"Hs  zRPCServer.__init__cCsdS)z@Override TCPServer method, no bind() phase for connecting entityNr )r#r r r server_bindMszRPCServer.server_bindcCs|jj|jdS)zOverride TCPServer method, connect() instead of listen() Due to the reversed connection, self.server_address is actually the address of the Idle Client to which we are connecting. N)socketZconnectserver_address)r#r r r server_activateQszRPCServer.server_activatecCs|j|jfS)z:Override TCPServer method, return already connected socket)r&r')r#r r r get_requestZszRPCServer.get_requestc CsyWntk rYntj}tdddd|tdd|tdtjjd|td|d|tdt|d|tj d|td d|tddd|t j d YnXd S) zOverride TCPServer method Error message goes to __stderr__. No error message if exiting normally or socket raised EOF. Other exceptions not handled in server code will cause os._exit.  -(filezUnhandled server exception!z Thread: %szClient Address: z Request: z# *** Unrecoverable, server exiting!rN) SystemExitsys __stderr__print threadingcurrent_threadnamerepr traceback print_excos_exit)r#requestZclient_addressZerfr r r handle_error^s  zRPCServer.handle_error)rrrr"r%r(r)r;r r r r rFs   rc@sreZdZdZddddZddZddZd d Zd d Zd dZ ddZ ddZ ddZ ddZ ddZddZddZddZdd Zd!d"Zd#d$Zd%d&Zd'd(Zd)d*Zd+Zd,ZdZd-d.Zd/d0Zd1d2Zd3d4Zd5d6Zd7d8Z d9d:Z!dS);SocketIOrNcCsdtj|_|dk r'||_n||_|dkrEt}n||_i|_i|_dS)N) r2r3 sockthread debuggingsock objecttableobjtable responsescvars)r#r?rAr>r r r r"s       zSocketIO.__init__cCs/|j}d|_|dk r+|jndS)N)r?close)r#r?r r r rDs   zSocketIO.closecCstjddS)z!override for specific exit actionrN)r8r9)r#r r r exithookszSocketIO.exithookcGsi|js dS|jdttjj}x"|D]}|dt|}q4Wt|dtjdS)N r-) r>locationstrr2r3r4r1r/r0)r#argssar r r debugs    zSocketIO.debugcCs||j|}|j|}|jd|j|<|j|jq!W|jdS)z+action taken upon link being closed by peerrr}N)zEOFN)EOFhookrLrCrrBrrrE)r#keyrr r r rs      zSocketIO.handle_EOFcCsdS)zBClasses using rpc client/server can override to augment EOF actionNr )r#r r r rszSocketIO.EOFhook)"rrrrr"rDrErLrOrQrorrrtrprsrqr{r~rrzrrrurwrrrrrrrrrrr r r r r<}s>       .               I r<c@seZdZdS)raN)rrrr r r r ras racCs t|}|t|rGr"rrr r r r rs   rc@sReZdZdZdZdZejejddZ ddZ dd Z d S) RPCClientFz#CrcCs9tj|||_|jj||jjddS)Nr)r&listening_sockZbindZlisten)r#addressZfamilytyper r r r"szRPCClient.__init__cCsz|jj\}}|jr7td|dtjn|dtkrZtj||ntd|dtjt dS)Nz****** Connection request from r-rz** Invalid host: ) racceptr>r1r/r0 LOCALHOSTr<r"re)r#Z working_sockrr r r rs zRPCClient.acceptcCs t||S)N)r)r#rMr r r rszRPCClient.get_remote_proxyN) rrrr>rGrr&ZAF_INETZ SOCK_STREAMr"rrr r r r rs  rc@sLeZdZdZdZddZddZddZdd ZdS) rNcCs||_||_dS)N)sockiorM)r#rrMr r r r"&s zRPCProxy.__init__cCs|jdkr|jn|jj|rDt|j|j|S|jdkr`|jn||jkr|jj|jd|fi}|St |dS)N__getattribute__) _RPCProxy__methods_RPCProxy__getmethodsr MethodProxyrrM_RPCProxy__attributes_RPCProxy__getattributesrrr)r#r4valuer r r __getattr__*s  zRPCProxy.__getattr__cCs%|jj|jdfi|_dS)NrV)rrrrMr)r#r r r Z__getattributes8szRPCProxy.__getattributescCs%|jj|jdfi|_dS)NrT)rrrrMr)r#r r r Z __getmethods<szRPCProxy.__getmethods) rrrrrr"rrrr r r r r!s    rcCsvx<t|D].}t||}t|r d||sJ                  2m