How to connect to socket.io using python - python

I want to connect to Socket.IO server using python. Is there a way to do it?
I have tried websocket-client as suggested in this answer.
ws = create_connection("ws://example.com:1000/socket.io/")
That code throws this exception
websocket._exceptions.WebSocketConnectionClosedException: Connection is already closed.
I feel like I am missing parameters because the JS client connection URL looks like this:
ws://example.com:1000/socket.io/?EIO=3&transport=websocket&sid=CHARSANDNUMBERS

You just need to use the url from the JS client, possibly without the sid:
ws = create_connection("ws://example.com:1000/socket.io/?EIO=3&transport=websocket")
From my personal experience I didn't have that sid param, but just try to add it if it doesn't work without.

You can look at this: socketIO-client
As an example,
from socketIO_client import SocketIO
socketIO = SocketIO('localhost', 8000)

Related

Connect to Lightstremer with Python for IG API streaming

I am trying to connect to Lightstremer to use the IG API streaming, and I would like to use the websockets library.
I am wondering if it is possible.
What I am struggling with is how to get the URI to use to create the connection with the server.
I can get the lightstreamer endpoint from IG and it looks like 'http://demo-apd.marketdatasystem.com'.
If I run
import websockets
import asyncio
ws_url = 'http://demo-apd.marketdatasystem.com/lighstreamer'
connection = await websockets.connect(ws_url)
I get the error https://demo-apd.marketdatasystems.com/lighstreamer isn't a valid URI
If I change the code below
ws_url = 'wss://demo-apd.marketdatasystem.com/lighstreamer'
connection = await websockets.connect(ws_url)
I have a message of failed connection
thanks for your time
Think you need to use a Lightstreamer client instead of WebSockets. As I understand it, they are two different protocols.
Official Python client from Lightstreamer - https://sdk.lightstreamer.com/ls-python-client/1.0.1/api/intro.html

Cannot connect to Binance websocket. I get: WebSocketBadStatusException: Handshake status 400 Bad Request

I am trying to connect to the Binance websocket stream.
Following their documentation I use the below code to establish the connection:
from websocket import create_connection
ws = create_connection('wss://fstream.binance.com/')
When running it though, I get the following error:
WebSocketBadStatusException: Handshake status 400 Bad Request
I could not find any info on the web about this error.
Does anyone know how to fix this?
This Point is somewhat unclear in the Binance API-Docs.
The base urls for the futures are:
wss://fstream.binance.com
wss://fstream3.binance.com
But if you just connect to these base urls you get the mentioned exception.
You should complement the url-strings to
wss://fstream.binance.com/ws
wss://fstream3.binance.com/ws
This is the same for spot markets and all the other websockets. Always put a "/ws" at the end.
You could also start subscribing with the connection-url then it looks like this spot-market example:
wss://stream.binance.com:9443/ws/btcusdt#aggTrade
(But i think connecting just with "/ws" and then live subscribing/unsubscribing as explained in the docs to streams is the better way.)
I gotta say it took me a long time to figure the solution out but here it goes.
Binance API documentation should be edited because it is missing the port for the fstream.binance.com
The port is 443.
So you should use
"fstream.binance.com:443" instead of
"fstream.binance.com".
Hope it helps. (Hell yeah it does!)
You could install python-binance and use the BinanceSocketManager
python -m pip install python-binance
Use the following code I found here
import time
from binance.client import Client # Import the Binance Client
from binance.websockets import BinanceSocketManager # Import the Binance Socket Manager
# Although fine for tutorial purposes, your API Keys should never be placed directly in the script like below.
# You should use a config file (cfg or yaml) to store them and reference when needed.
PUBLIC = '<YOUR-PUBLIC-KEY>'
SECRET = '<YOUR-SECRET-KEY>'
# Instantiate a Client
client = Client(api_key=PUBLIC, api_secret=SECRET)
# Instantiate a BinanceSocketManager, passing in the client that you instantiated
bm = BinanceSocketManager(client)
# This is our callback function. For now, it just prints messages as they come.
def handle_message(msg):
print(msg)
# Start trade socket with 'ETHBTC' and use handle_message to.. handle the message.
conn_key = bm.start_trade_socket('ETHBTC', handle_message)
# then start the socket manager
bm.start()
# let some data flow..
time.sleep(10)
# stop the socket manager
bm.stop_socket(conn_key)
You are missing the path on websocket connect!
Take a look to the binance api docs:
https://binance-docs.github.io/apidocs/futures/en/#websocket-market-streams
python-binance does not support websocket to binance futures endpoints, so you can use unicorn-binance-websocket-api instead, here is an example for future endpoints:
https://github.com/oliver-zehentleitner/unicorn-binance-websocket-api/blob/master/example_binance_futures.py

pysimplesoap web service return connection refused

I've created some web services using pysimplesoap like on this documentation:
https://code.google.com/p/pysimplesoap/wiki/SoapServer
When I tested it, I called it like this:
from SOAPpy import SOAPProxy
from SOAPpy import Types
namespace = "http://localhost:8008"
url = "http://localhost:8008"
proxy = SOAPProxy(url, namespace)
response = proxy.dummy(times=5, name="test")
print response
And it worked for all of my web services, but when I try to call it by using an library which is needed to specify the WSDL, it returns "Could not connect to host".
To solve my problem, I used the object ".wsdl()" to generate the correct WSDL and saved it into a file, the WSDL generated by default wasn't correct, was missing variable types and the correct server address...
The server name localhost is only meaningful on your computer. Once outside, other computers won't be able to see it.
1) find out your external IP, with http://www.whatismyip.com/ or another service. Note that IPs change over time.
2) plug the IP in to http://www.soapclient.com/soaptest.html
If your local service is answering IP requests as well as from localhost, you're done!

TestLink xmlrpc API (via Python) 404 not found

I'm trying to connect to TestLink via the xmlrpc API. I've set the following in TestLink's config.inc.php:
$tlCfg->api->enabled = TRUE;
$tlCfg->exec_cfg->enabled_test_automation = ENABLED;
and restarted the apache sever. I tried to connect the TestLink server via the python package TestLink-API-Python-client (https://github.com/orenault/TestLink-API-Python-client)
from testlink import TestlinkAPIClient, TestLinkHelper
import sys
URL = 'http://MYSERVER/testlink/lib/api/xmlrpc.php'
DevKey = 'MYKEY'
tl_helper = TestLinkHelper()
myTestLink = tl_helper.connect(TestlinkAPIClient)
myTestLink.__init__(URL, DEVKEY)
myTestLink.checkDevKey()
And then I receive a TLConnectionError, stating my url, and 404 Not Found...
Does anyone have any idea?
Thanks.
I didn't solve it.
I reverted to working on the TestLink DB directly. I'm sure it's more fragile than using the API, but it works...
If you are still looking for help, this code worked for me:
set TESTLINK_API_PYTHON_SERVER_URL=http://[YOURSERVER]/testlink/lib/api/xmlrpc/v1/xmlrpc.php
set TESTLINK_API_PYTHON_DEVKEY=[Users devKey generated by TestLink]
python
import testlink
tls = testlink.TestLinkHelper().connect(testlink.TestlinkAPIClient)
tls.countProjects()
Check out TestLink API Documentation to learn more
In a glance your XML-RPC URL seems wrong. It should be
http://YOURSERVER/testlink/lib/api/xmlrpc/v1/xmlrpc.php

Web sockets / Tornado - Notify client on database update

I'm trying to use a Tornado web socket server to notify my user when changes are made to a database in realtime. I was hoping to use HTML5 web sockets for this, even though most browsers don't support them. None of the demos that come with the Tornado package use web sockets and they are not mentioned in the documentation, so I have no idea how to get started. The few examples I could find on google either don't work or are poorly documented.
Does anyone have any examples of how I can use Tornado to push data to a client when a MySQL database has been updated or something similar I can use to learn from?
A Lee's answer is a good one, you probably want socket.io if you need to support older browsers.
Websockets are very easy in tornado though:
import tornado.websocket
class EchoWebSocket(tornado.websocket.WebSocketHandler):
def open(self):
print "WebSocket opened"
def on_message(self, message):
self.write_message(u"You said: " + message)
def on_close(self):
print "WebSocket closed"
Then route it as any other handler, and include Websocket Javascript in your views:
var ws = new WebSocket("ws://localhost:8888/websocket");
ws.onopen = function() {
ws.send("Hello, world");
};
ws.onmessage = function (evt) {
alert(evt.data);
};
For more info, see the source: https://github.com/facebook/tornado/blob/master/tornado/websocket.py
I've had success using the socket.io client and tornadio on the server end. Socket.IO provides an abstraction over websockets and provides fallbacks if websockets aren't supported by the browser (long polling, flash socket, etc.).
In order to use it you just need to write a tornadio script a la the tornadio documentation that monitors your database and then include the socket.io JavaScript in your web pages and have it establish a connection to wherever your tornadio server resides at the URL route you specified in your tornadio script.
This post using websockets and redis covers the basic idea pretty well.

Categories