Flask, Python 3, and WebSockets - python

How can I serve a websockets endpoint from Flask running on Python 3? This seems near impossible. Note, I do not want SocketsIO or anything like that. Just want to be able, from plain JavaScript following the very simple HTML5 standard, connect to to a Flask endpoint and upgrade it to a Websocket for bidirectional communication. Example JS below
var ws = new WebSocket("ws://localhost:5000/echo");
I assumed this would be a simple matter of pip installing the right module, but no. Any ideas? Should I write my own Python module for this?

Unfortunately this is currently not supported. Possible workarounds below
Go node.js.
Go python2
Fix the existing flask-sockets module, issue here.
Take it on yourself to write a python3 aware python module

Related

Best way to make a web interface for python script

I've made a small python script to scrap the web. I would like to make a nice and simple web interface where the user can enter data to search for and have the result displayed as a list.
I understand that there's many different ways to do that but don't know which one would be the best in my case.
I would like something :
Really simple and light
Running locally with the less dependencies possible.
So far I've thinking about :
A NodeJS server displaying content and executing the script
A web framework in Python (web.py, Flask, Django..?)
A local webserver (XAMPP) and cgi
Please note that I don't know much about web in python but I'm a bit used to NodeJS.
What would you recommend ?
Thanks, Victor
Personally I prefer gevent, bottle or Flask, and some front end framework like bootstrap or framework7.
Gevent easily makes it asynchronous and has websockets built right in, and bottle is the easiest (and fastest) way to build a web app or api.
Socket.io ist Very easy to send Data between Websites and scripts.
The Website Connect with the Socket.io Server and Inside the Server the Python Script can be executed

How to receive the Events from .NET using Python

I want to consume the events/Signals exposed by the Application via .NET SignalR.
My requirement is to receive those signals using Python.
Kindly help me out.
A similar question was asked here:
Using SignalR server from Python code
There are no Python SignalR libraries available, so your only option would be to port a lightweight version of SignalR to python yourself. See https://github.com/davidfowl/SignalR.Lite
Obviously this is not a trivial undertaking!
I just wanted to offer another approach. Maybe the simpler solution would be to execute the SignalrR javascript library in python and just create a translational layer to get it back into python objects.
I do not have code proving this, but I was hoping this would encourage someone in the future to try it.

Can I run Python 2.7 Libraries (Impacket, Pcapy) on Django

Hi I just want to ask if its possible to run Impacket to Django,
On my project I am already done on my sniffing and parsing using Impacket and Pcapy but my clients requested that the GUI will be web based. I picked Django because its the most widely used and all but I am having doubts that it can run my Libraries.
For starters can Django open my NIC in Ubuntu and have access to sniff on it?
or is it better for me to use Flask for from what I read flask is being run on the Python Console Application, from what I understood I will install a HTTP Server in the Project then the Python Console will be like a Controller (MVC) to my GUI which is Flask.
You can run anything from Django, it just provides a framework to get stuff to the web.
As long as the Django application is running as a user which has privileges to access your NIC there won't be an issue with that.
You can simply call your code you already have from the Django Views.
The time this stuff takes may be too long for a web request so you may need to pass some stuff down a message queue and look up the results. Look at Celery for that purpose.
I do prefer Flask over Django but it doesn't matter what you use.
Just remember Django is another library it all still runs inside Python :)

python send commands to interpreter from script and get result

My goal is to create a webpage the uses python and flask. I would like to have the ability to enter some commands into this web page, submit them to flask which will execute the commands in a python interpreter. Flask will then retrieve the results and send them back to the web page for presentation.
How can this be done? I currently am able to execute unix shell commands but cannot for the life of me figure out how to send commands to a python interpreter and retrieve the results.
I guess I should have clarified the purpose. This is for an internal web application for the company that I work for. Only employees will be able to access it while on our network and only after proper authentication has been performed. The employees need a way to have an interactive shell with the particular machine that they are logged in to through their web browser.
SOLUTION:
I found a nice module name Pexpect that does exactly what I want. Thank you for all of your suggestions.
The following code uses flask and pexpect module to call python interpreter.
#app.route('/console')
def console():
import pexpect
child = pexpect.spawn('python')
child.expect('\n>>>')
child.sendline('import os')
child.expect('\n>>>')
Well, you can invoke python in the same way as you do a shell script and pass python code to it using the -c option. See http://docs.python.org/2/using/cmdline.html
However, this is unbelievably insecure and I would not recommend doing it in a web app!
If you are set on this, read up on restricted execution in Python http://docs.python.org/2/library/restricted.html
The Werkzeug debugger that is included with Flask can do this when there is an exception, so I think it should be fairly easy to base your solution on that code, or at least to learn how Werkzeug does it.
The debugger server side code is here. The client side is here.
I hope you have a good reason to do this, you normally do not want random people to execute code on your server.

socket.io like for Python 2.7 + Bottle framework

I am developing my web app with Python 2.7 + Bottle. Everything is great and python is an amazing language coming from ASP.NET. I am building a web application that needs to use real-time client/server communication and socket.io for node.js comes to mind.
I wanted to know how can I implement socket.io-like using Python + bottle. I've read this article on bottle, but I can't still seem to understand how it works - what I need to install, and how all works out (code examples?).
I really need that for my next web application but need help in understanding what I need to put in my project in order for it to work. I have no problem working with 'preview' codes which aren't stable release yet. I'm developing on Windows platform. Thanks.
I also want to know if its scalabe. whether I can use redis in the back so all calls will be sync when running my website on several servers, so when one client send data, all the other clients connected to the other servers will get it to.
maybe websocket can help you,many modern browser support this protocol,but bottle.py don't support it now,you can get some idea from tornado.websocket and some answer here
cause every connection can be saved,so i guess it can run on several servers,but i never implement.
since bottle is micro framework,you should do something by yourself.

Categories