Connect to web socket (wss) with standard modules in Python? - python

I have used Python external package, websocket-client to connect to a RESTful service that sends events.
from websocket import create_connection
ws = create_connection("wss://machine:port/servicename/subscribe")
for event in ws:
print event
...getting events printed
I wonder whether the same functionality can be implemented using the core Python 2.7 or Python 3.5, without installing the external websocket-client package or any other 3rd party Python package.
I have searched the Internet and those examples of code using Python socket module I've seen, refer to connections via http or tcp whereas I need to establish connection via wss.

Very late, but for anyone who might stumble here...Web Sockets protocol is built on TCP so it is expected that implementations of the protocol establish connections via TCP. Also, Web Socket's opening handshake is done following HTTP. This would also explain why Alex saw HTTP connections. If you want to see how to implement one from scratch, install web socket modules (autobahn, which in turn would lead to installing and reading Twisted and asyncio modules, or wampy) and dive into the code. Would recommend reading the Web Sockets protocol specifications at https://www.rfc-editor.org/rfc/rfc6455 before tackling the modules.

Related

PPPOE Discovery Stage with Python Sockets

Is there any way of implementing PPPOE discovery stage, i.e. sending PADI receiving PADO and all using Python & Sockets? Not allowed to use Scapy module.
I tried to look all over the internet but can't find anything.

How do I make a Python socket server that is connected to another Python socket server?

I'm creating a Python socket server that is supposed to handle requests from a frontend JavaScript. The Python backend needs to be simultaneously connected to another socket server to download real time crypto prices. How would I go about doing that?
I've tried many things, but I'm not able to get both the server and client functionality on my Python working.

How do I communicate to NodeMCU module through python?

Basically I want to be able to send instructions for a bot over WiFi using the nodeMCU module, but I cant find any detailed functions list that can help me.
I've already tried setting up a server on the NodeMCU, and then a html webpage can then be created on my laptop with buttons for instructions from this and it works, but when I try to to the same on python, (send GET requests through python) I get this error on trying to do more than one request:
ConnectionError: ('Connection aborted.', ConnectionAbortedError(10053, 'An established connection was aborted by the software in your host machine', None, 10053, None))
Also I think the converse would be better suited for my case, a server on the laptop and client as the module, but I don't know where to start.
I coded the on the module using arduino IDE and on my laptop I'm using spyder. For sending requests and stuff I'm using the requests package.
You can create HTTP endpoints in nodeMCU and call them from your python script.
For this to work, both the nodeMCU and the machine running the python script has to be in the same network. and the IP address of the nodeMCU should be correctly added in the python script.
If you wanted to use your laptop as the server, I would suggest going with an MQTT broker such as mosquito, and communicate between the nodeMCU and the python script using the MQTT protocol.

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.

NodeJS - SocketIO over SSL with websocket transport

I have a NodeJS-socketIO server that has clients listening from JS, PHP & Python. It works like a charm when the communication happens over plain HTTP/WS channel.
Now, when i try to secure this communication, the websocket transport is not working anymore. It falls back to xhr-polling(long polling) transport. Xhr-polling still works for JS client but not on python which purely depends on socket transport.
Things i tried:
On node, Using https(with commercial certificates) instead of http - Works good for serving pages via Node but not for socketIO
Proxy via HAProxy (1.15-dev19). From HTTPS(HAProxy) to HTTP(Node). Couldn't get Websocket transport working and it falls back to xhr-polling on JS. Python gets 502 on handshake.
Proxy via STunnel (for HTTPS) -> HAProxy(Websocket Proxy) -> Node(SocketIO) - This doesnt work either. Python client still gets 502 on handshake.
Proxy via Stunnel(HTTPS) -> Node(SocketIO) - This doesnt work too. Not sure if STunnel support websocket proxy
node-http-proxy : Throws 500(An error has occurred: {"code":"ECONNRESET"}) on websocket and falls back to xhr-polling
Im sure its a common use case and there is a solution exist. Would really appreciate any help.
Thanks in advance!
My case seems to be a rare one. I built this whole environment on a EC2 instance based on Amazon Linux. As almost all the yum packages are not up to date, i had to install pretty much every yum packages from source. By doing so i could have missed configuration unchanged/added. Or HAProxy required lib could have been not the latest.
In any case, i tried building the environment again on ubuntu 12.04 based EC2 instance. HAProxy worked like a charm with a bit of configuration tweaks. I can now connect my SocketIO server from JS, Python & PHP over SSL without any problem. I could also create a Secured TCP Amazon ELB that listens on 443 and proxy it to non-standard port (8xxx).
Let me know if anyone else encounters a similar problem, I will be happy to help!

Categories

Resources