Embedding a remote Python shell in an application - python

You can embed the IPython shell inside of your application so that it launches the shell in the foreground. Is there a way to embed a telnet server in a python app so that you can telnet to a certain port and launch a remote IPython shell?
Any tips for redirecting the input/output streams for IPython or how to hook it up to a telnet server library or recommendations for other libraries that could be used to implement this are much appreciated.

Python includes a telnet client, but not a telnet server. You can implement a telnet server using Twisted. Here's an example. As for hooking these things together, that's up to you.

Use Twisted Manhole. Docs are a bit lacking, but it's easy enough to set up a telnet-based remote server and it comes with a GTK-based GUI.
Main Twisted site
twisted.manhole API docs

I think you should base your server class on the SocketServer class from the standard library. You'll need to write a RequestHandler to read and echo input but a lot of the heavy lifting is already done for you.
You can use the ThreadingMixIn to make the server multi-threaded very easily.

Try to use xmlrpc namespace

Related

Sending commands to and receiving their output from a running Python application (via remote connection)

I have a long-running server application written in Python 3, which I would like to interactively debug from time to time. For that I would like to run Python commands "within" the server application process, inspect the values of global variables etc., like in a REPL or the standardd Python console.
It seems the Python standard library's code module and its InteractiveConsole class seems to be what I am looking for. I was thinking of running that in a separate thread so that the main application is not blocked while I communicate with it.
However, it seems that class provides interaction via standard input and output. That might not be exactly what I need. Is there a way to make that interactive console listen / connect to a socket and send input and output through this socket, so that I can connect to the console via a TCP connection?
Or is there another, better way to implement my requirement without this code module?

Telnet client control panel python

I am working on Jasmin SMS gateway and I want to use its CLI module using my python script. It serves this module as telnet. I am not sure ho to send and receive commands from telnet. I have tried telnetlib but I do not know how to use it properly. Jamsin SMS Gateway can be found here:
https://jasmin.readthedocs.io/en/latest/management/jcli/index.html#architecture
I found pexpect library that works great with telnet and ssh clients. I can perform all the tasks of Jamsin's CLI module using my scripts.
There is also a web interface available written in Django and work perfectly with jasmin using pexpect. Its called JasminWebPanel.

communicating with Python socket server (Chrome extension)

i am trying to write a simple extension in chrome which sends data to a TCP socket server in python.
Unfortunately, extensions cannot use sockets library in chrome.
is there any simple way to communicate with a TCP socket server in python?
Alternatively, is there any way to communicate with python at all?
I think Native Messaging is what you want.
You can find a example code snippet in the Example section of this page.
It uses native messaging to communicate with a Python script.

Using client and server websockets in same python script

I am new to Python - and work on Slackware Linux with Python 3.4.3. I prefer simple no-dependency solutions within one single python script.
I am building a demonized server program (A) which I need to access through both a regular shell CLI and GUIs in my web browser: it serves various files, uses a corresponding database and updates a firefox tab through python's WEBBROWSER function. Currently, I access process (A) via the CLI or a threaded network socket. This all started to work in a localhost scenario with all processes running on one machine.
Now, it turns out that the WebSocket protocol would render my setup dramatically simpler and cut short on traditional flow protocols using Apache and complex frameworks as middlemen.
1st central question: How do I access daemon (A) with websockets from the CLI? I thought about firing up a non-daemon version of my server program, now called (B), and send a program call to its (A) counterpart via the WebSocket HTTP protocol. This would make process (B) a websocket CLIENT, and process (A) a websocket SERVER. Is such a communication at all possible today?
2nd question: Which is the best suited template solution for this scenario - that works with python 3.4.3 ?! I started to play with Pithikos' very sleek python-websocket-server template (see https://github.com/Pithikos/python-websocket-server) but I am unable to use it as CLIENT (initiating the network call) to call its SERVER equivalent (receiving the call while residing in a daemonized process).
Problem 'solved': I gave up on the zero-dependency zero-library idea :
pip install websockets
https://websockets.readthedocs.io
It works like a charm. The WebSocket server sits in the daemon process and receives and processes WebSocket client calls that come from the CLI processes and from the HTML GUIs.

python: twisted app with interacive python shell

Would it be possible to write a twisted-powered application that opens an interactive shell, e.g. to tweak the business objects served via the twisted protocol layer?
My current problem is that the reactor.run() blocks the application, and the IPython.embed() is only run after the reactor has finished.
Unless you specifically need extras that are only in ipython, you really should check out manhole (twistedmatrix example code)
It lets you access a python interactive shell that is natively running in the twisted reactor. Normally the shell is accessed via telnet or ssh, but if you wanted to be creative you could wire it up any way you want.
... Meanwhile I found an answer to my question: http://twistedmatrix.com/documents/current/core/howto/threading.html
def interact():
import IPython
IPython.embed()
reactor.callInThread(interact)
reactor.run()

Categories

Resources