0
# Classic Mode
1
2
Classic RPyC functionality providing direct remote Python execution, module access, and file transfer capabilities. Classic mode enables transparent remote code execution and filesystem operations, making the remote Python interpreter accessible as if it were local.
3
4
## Capabilities
5
6
### Connection Functions
7
8
Classic mode connection functions that automatically use ClassicService for remote Python execution.
9
10
```python { .api }
11
def connect(host, port=18812, ipv6=False, keepalive=False):
12
"""
13
Connect to classic RPyC server.
14
15
Parameters:
16
- host (str): Server hostname or IP address
17
- port (int): Server port (default 18812)
18
- ipv6 (bool): Use IPv6 if True
19
- keepalive (bool): Enable TCP keepalive
20
21
Returns:
22
Connection: Classic RPyC connection with remote Python access
23
"""
24
25
def ssl_connect(host, port=18821, keyfile=None, certfile=None, ca_certs=None,
26
cert_reqs=None, ssl_version=None, ciphers=None, ipv6=False, keepalive=False):
27
"""
28
Connect to classic RPyC server over SSL.
29
30
Parameters:
31
- host (str): Server hostname or IP address
32
- port (int): Server SSL port (default 18821)
33
- keyfile (str): Path to private key file
34
- certfile (str): Path to certificate file
35
- ca_certs (str): Path to CA certificates file
36
- cert_reqs: Certificate requirements
37
- ssl_version: SSL version to use
38
- ciphers (str): Cipher suites to use
39
- ipv6 (bool): Use IPv6 if True
40
- keepalive (bool): Enable TCP keepalive
41
42
Returns:
43
Connection: Secure classic RPyC connection
44
"""
45
46
def unix_connect(path):
47
"""
48
Connect to classic RPyC server via Unix socket.
49
50
Parameters:
51
- path (str): Path to Unix socket file
52
53
Returns:
54
Connection: Classic RPyC connection over Unix socket
55
"""
56
57
def ssh_connect(remote_machine, remote_port):
58
"""
59
Connect to classic RPyC server through SSH tunnel.
60
61
Parameters:
62
- remote_machine: SSH connection object
63
- remote_port (int): Remote port number
64
65
Returns:
66
Connection: Classic RPyC connection through SSH
67
"""
68
69
def connect_subproc(server_file=None):
70
"""
71
Start classic RPyC server as subprocess and connect.
72
73
Parameters:
74
- server_file (str): Path to server script (optional)
75
76
Returns:
77
Connection: Classic RPyC connection to subprocess
78
"""
79
80
def connect_thread():
81
"""
82
Create in-thread classic RPyC connection.
83
84
Returns:
85
Connection: Classic RPyC connection in thread
86
"""
87
```
88
89
### Remote Code Execution
90
91
Direct execution of Python code on the remote interpreter.
92
93
```python { .api }
94
# Connection methods for code execution
95
def execute(self, code):
96
"""
97
Execute Python code on remote interpreter.
98
99
Parameters:
100
- code (str): Python code to execute
101
102
Returns:
103
None
104
105
Note: Access via conn.execute(code)
106
"""
107
108
def eval(self, expression):
109
"""
110
Evaluate Python expression on remote interpreter.
111
112
Parameters:
113
- expression (str): Python expression to evaluate
114
115
Returns:
116
Result of expression evaluation
117
118
Note: Access via conn.eval(expression)
119
"""
120
```
121
122
### Remote Module Access
123
124
Access to remote Python modules and built-in functions.
125
126
```python { .api }
127
# Connection properties for module access
128
@property
129
def modules(self):
130
"""
131
Access to remote Python modules.
132
133
Returns:
134
ModuleNamespace: Proxy to remote sys.modules
135
136
Usage:
137
- conn.modules.os.listdir('/tmp')
138
- conn.modules['xml.dom.minidom'].parseString('<root/>')
139
"""
140
141
@property
142
def builtin(self):
143
"""
144
Access to remote built-in functions.
145
146
Returns:
147
ModuleNamespace: Proxy to remote builtins
148
149
Usage:
150
- conn.builtin.open('/path/file', 'r')
151
- conn.builtin.len([1, 2, 3])
152
"""
153
154
@property
155
def builtins(self):
156
"""Alias for builtin property"""
157
```
158
159
### File Transfer Operations
160
161
High-level file and directory transfer between local and remote systems.
162
163
```python { .api }
164
def upload(conn, localpath, remotepath, filter=None, ignore_invalid=False, chunk_size=8192):
165
"""
166
Upload files or directories to remote system.
167
168
Parameters:
169
- conn: RPyC connection
170
- localpath (str): Local file or directory path
171
- remotepath (str): Remote destination path
172
- filter (callable): Optional filter function for files
173
- ignore_invalid (bool): Skip invalid files if True
174
- chunk_size (int): Transfer chunk size in bytes
175
"""
176
177
def upload_file(conn, localpath, remotepath, chunk_size=8192):
178
"""
179
Upload single file to remote system.
180
181
Parameters:
182
- conn: RPyC connection
183
- localpath (str): Local file path
184
- remotepath (str): Remote file path
185
- chunk_size (int): Transfer chunk size in bytes
186
"""
187
188
def upload_dir(conn, localpath, remotepath, filter=None, chunk_size=8192):
189
"""
190
Upload directory to remote system.
191
192
Parameters:
193
- conn: RPyC connection
194
- localpath (str): Local directory path
195
- remotepath (str): Remote directory path
196
- filter (callable): Optional filter function for files
197
- chunk_size (int): Transfer chunk size in bytes
198
"""
199
200
def download(conn, remotepath, localpath, filter=None, ignore_invalid=False, chunk_size=8192):
201
"""
202
Download files or directories from remote system.
203
204
Parameters:
205
- conn: RPyC connection
206
- remotepath (str): Remote file or directory path
207
- localpath (str): Local destination path
208
- filter (callable): Optional filter function for files
209
- ignore_invalid (bool): Skip invalid files if True
210
- chunk_size (int): Transfer chunk size in bytes
211
"""
212
213
def download_file(conn, remotepath, localpath, chunk_size=8192):
214
"""
215
Download single file from remote system.
216
217
Parameters:
218
- conn: RPyC connection
219
- remotepath (str): Remote file path
220
- localpath (str): Local file path
221
- chunk_size (int): Transfer chunk size in bytes
222
"""
223
224
def download_dir(conn, remotepath, localpath, filter=None, chunk_size=8192):
225
"""
226
Download directory from remote system.
227
228
Parameters:
229
- conn: RPyC connection
230
- remotepath (str): Remote directory path
231
- localpath (str): Local directory path
232
- filter (callable): Optional filter function for files
233
- chunk_size (int): Transfer chunk size in bytes
234
"""
235
```
236
237
### Package and Object Transfer
238
239
Transfer Python packages and objects between local and remote systems.
240
241
```python { .api }
242
def upload_package(conn, module, remotepath=None, chunk_size=8192):
243
"""
244
Upload Python package to remote system.
245
246
Parameters:
247
- conn: RPyC connection
248
- module: Python module to upload
249
- remotepath (str): Remote destination path (optional)
250
- chunk_size (int): Transfer chunk size in bytes
251
"""
252
253
def obtain(proxy):
254
"""
255
Transfer remote object to local system.
256
257
Parameters:
258
- proxy: Remote object proxy (netref)
259
260
Returns:
261
Local copy of remote object
262
"""
263
264
def deliver(conn, localobj):
265
"""
266
Transfer local object to remote system.
267
268
Parameters:
269
- conn: RPyC connection
270
- localobj: Local object to transfer
271
272
Returns:
273
Remote proxy to transferred object
274
"""
275
276
def teleport_function(conn, func, globals=None, def_=True):
277
"""
278
Transfer function to remote system for execution.
279
280
Parameters:
281
- conn: RPyC connection
282
- func: Function to transfer
283
- globals (dict): Global variables for function
284
- def_ (bool): Define function remotely if True
285
286
Returns:
287
Remote proxy to transferred function
288
"""
289
```
290
291
### Interactive Features
292
293
Interactive debugging and development tools.
294
295
```python { .api }
296
def redirected_stdio(conn):
297
"""
298
Context manager redirecting stdin/stdout to remote system.
299
300
Parameters:
301
- conn: RPyC connection
302
303
Returns:
304
Context manager for stdio redirection
305
306
Usage:
307
with redirected_stdio(conn):
308
# Local print() outputs to remote stdout
309
print("This appears on remote system")
310
"""
311
312
def interact(conn, namespace=None):
313
"""
314
Start interactive session with remote Python interpreter.
315
316
Parameters:
317
- conn: RPyC connection
318
- namespace (dict): Local namespace for interaction
319
"""
320
321
def pm(conn):
322
"""
323
Start post-mortem debugging session on remote system.
324
325
Parameters:
326
- conn: RPyC connection
327
"""
328
```
329
330
## Examples
331
332
### Basic Remote Execution
333
334
```python
335
import rpyc.classic as rpyc
336
337
# Connect to classic server
338
conn = rpyc.connect('remote-host')
339
340
# Execute Python code
341
conn.execute('x = 5 * 10')
342
conn.execute('import math')
343
conn.execute('y = math.sqrt(x)')
344
345
# Evaluate expressions
346
result = conn.eval('y')
347
print(f"Result: {result}") # Result: 7.07...
348
349
conn.close()
350
```
351
352
### Remote Module Access
353
354
```python
355
import rpyc.classic as rpyc
356
357
conn = rpyc.connect('remote-host')
358
359
# Access remote modules
360
files = conn.modules.os.listdir('/tmp')
361
print("Remote /tmp contents:", files)
362
363
# Use remote built-ins
364
remote_file = conn.builtin.open('/etc/hostname', 'r')
365
hostname = remote_file.read().strip()
366
remote_file.close()
367
print("Remote hostname:", hostname)
368
369
# Work with remote data structures
370
remote_dict = conn.eval("{'key': 'value', 'number': 42}")
371
print("Remote dict keys:", list(remote_dict.keys()))
372
373
conn.close()
374
```
375
376
### File Transfer Operations
377
378
```python
379
import rpyc.classic as rpyc
380
381
conn = rpyc.connect('remote-host')
382
383
# Upload files
384
rpyc.upload(conn, 'local_file.txt', '/tmp/remote_file.txt')
385
rpyc.upload_dir(conn, 'local_dir/', '/tmp/remote_dir/')
386
387
# Download files
388
rpyc.download(conn, '/etc/passwd', 'downloaded_passwd.txt')
389
rpyc.download_dir(conn, '/var/log/', 'local_logs/')
390
391
# Upload Python package
392
import json
393
rpyc.upload_package(conn, json, '/tmp/json_module/')
394
395
conn.close()
396
```
397
398
### Object Transfer
399
400
```python
401
import rpyc.classic as rpyc
402
403
conn = rpyc.connect('remote-host')
404
405
# Create remote object
406
conn.execute('data = [1, 2, 3, 4, 5]')
407
remote_list = conn.eval('data')
408
409
# Bring remote object local
410
local_copy = rpyc.obtain(remote_list)
411
print("Local copy:", local_copy)
412
413
# Send local object to remote
414
local_dict = {'name': 'Alice', 'age': 30}
415
remote_proxy = rpyc.deliver(conn, local_dict)
416
417
# Use remote proxy
418
conn.execute('print("Remote dict:", delivered_dict)')
419
420
conn.close()
421
```
422
423
### Interactive Development
424
425
```python
426
import rpyc.classic as rpyc
427
428
conn = rpyc.connect('remote-host')
429
430
# Redirect local output to remote
431
with rpyc.redirected_stdio(conn):
432
print("This appears on remote stdout")
433
user_input = input("Enter something on remote: ")
434
435
# Start interactive session
436
rpyc.interact(conn, namespace={'myvar': 42})
437
438
# In case of remote errors, debug
439
try:
440
conn.execute('1/0') # This will cause error
441
except:
442
rpyc.pm(conn) # Start post-mortem debugging
443
444
conn.close()
445
```
446
447
### Function Teleportation
448
449
```python
450
import rpyc.classic as rpyc
451
452
conn = rpyc.connect('remote-host')
453
454
# Define function to teleport
455
def process_data(data):
456
return [x * 2 for x in data if x > 0]
457
458
# Teleport function to remote
459
remote_func = rpyc.teleport_function(conn, process_data)
460
461
# Use function remotely
462
conn.execute('test_data = [-1, 2, -3, 4, 5]')
463
result = conn.eval('process_data(test_data)')
464
print("Remote processing result:", list(result))
465
466
conn.close()
467
```
468
469
## Constants
470
471
```python { .api }
472
DEFAULT_SERVER_PORT = 18812 # Default classic server port
473
DEFAULT_SERVER_SSL_PORT = 18821 # Default classic SSL server port
474
STREAM_CHUNK = 8192 # Default chunk size for file transfers
475
```