I am trying to get the live prices of crypto tokens in binance.I used websockets for that.
Code:
import websockets
import asyncio
async def hello():
async with websockets.connect('wss://fstream.binance.com/ws/!markPrice#arr') as websocket:
print("connected!")
while True:
print("Debug")
greeting = await websocket.recv()
print(greeting)
await client.close_connection()
asyncio.run(hello())
This is code working fine in local linux machine (means it's printing the data coming from web socket which is being stored in greeting variable.)
At the same time without changing any code in that, I run that in aws ec2 ubuntu instance,it's doing nothing, just printing connected! and Debug. After that nothing print on console and no any error raised.
I installed web sockets latest version.
I was experiencing a very similar issue. I've found out it has something to do with the newest websocket library release. As a workaround, downgrading from 10.0 to 9.1 is what helped me.
Related
The following code only works in the browser. It does not work in node.js
let socket1 = io('http://localhost:3031/nsp')
socket1.on("connect", (error) => {
console.log("socket1: connect")
});
I am connecting to a netty-socketio (v1.7.7) java server. I am able to connect to the root namespace on both browser and node.js clients and everything works as expected. However, if I try to connect to a namespace, only the browser client can connect as expected.
Furthermore, I can see on the server that the node.js client (v2.4.0) is connecting to the root namespace when it should be connecting to the named namespace "nsp". As such, the clients join the root namespace and seemingly never join the "nsp" namespace.
On further inspection, the only event I can get to fire on the node.js client, when specifying a namespace, after connecting is "ping" all other events (connect,connect_error,error,reconnect...) never trigger.
Update: the above code works when connecting to a node.js server, so the issue appears to be with the netty-socketio server.
Here is how the netty server is initialized:
Configuration config = new Configuration();
config.setHostname("localhost");
config.setPort(3031);
server = new SocketIOServer(config);
server.addNamespace('/nsp')
I finally figured it out. I had to revert the node.js client version to 1.7.4 (https://www.npmjs.com/package/socket.io-client/v/1.7.4) to get it to work. Not sure why I was forced to use such an old client to be able to use namespaces.
This is my first time posting, and I'm in a difficult situation i'd consider somewhat unique.
My Goal and Whats happened so far:
Basically, I just need to pass integers from my desktop PC to a raspberry pi in "real time", preferably over a wireless connection. The code snippets below are versions of the "test program" that employ specifically what I'm having issues with. To achieve this, I'm using the python websockets module
The issue I'm facing is new to me, because previously this program worked entirely as intended, as I lived in a location where I owned my router in a house, but now I live in an apartment complex with special network restrictions (needing to register MAC addresses and such)
Previous Results (When I lived at the previous location)
I achieved the desired results of pasting random integers to the console as fast as possible that were generated from the server PC with the following programs below. Note that when I put i used the IP that could be easily found when you google "what is my IP"
SERVER SIDE (ON THE PC)
import asyncio
import websockets
import random
async def createdata():
return random.randint(0, 15000)
async def senddata(websocket, path):
number = await createdata()
print("sending data")
await websocket.send(str(number))
print(number)
start_server = websockets.serve(senddata, "<INSERTIPHERE>", 4421)
asyncio.get_event_loop().run_until_complete(start_server)
asyncio.get_event_loop().run_forever()
CLIENT SIDE (ON THE RASP PI)
import asyncio
import websockets
async def datarecieve():
uri = "ws://<INSERTIPHERE>:4421"
async with websockets.connect(uri) as websocket:
data = await websocket.recv()
print(data)
while True:
asyncio.get_event_loop().run_until_complete(datarecieve())
Results Now (Now that I live in the apartment complex)
I get the error thrown on the client side: ConnectionRefusedError: [Errno 111] Connect call failed (ipgoeshere, port). Here are the programs:
SERVER SIDE (ON THE PC)
import asyncio
import websockets
import random
async def createdata():
return random.randint(0, 15000)
async def senddata(websocket, path):
number = await createdata()
print("sending data")
await websocket.send(str(number))
print(number)
start_server = websockets.serve(senddata, "localhost", 4421)
asyncio.get_event_loop().run_until_complete(start_server)
asyncio.get_event_loop().run_forever()
CLIENT SIDE (ON THE RASP PI)
import asyncio
import websockets
async def datarecieve():
uri = "ws://<externalipofappartmentcomplex>:4421"
async with websockets.connect(uri) as websocket:
data = await websocket.recv()
print(data)
while True:
asyncio.get_event_loop().run_until_complete(datarecieve())
More Information About What I've Tried or What I'm Willing to Do
For the server side, I've also tried ips like: 127.0.0.1, 0.0.0.0, and the ipv4 assigned to the device (found in ipconfig in cmd)
For the Client side, I've also tried the ipv4 assigned to the server device
Opening up the port on the firewall of the server computer
I've considered just buying my own wireless router and hopefully finding luck that way
Thank you for any help.
I have a 3rd party server that runs SignalR. In order to connect to that server and read messages i have created a javascript code that should run from NodeJS on ec2 (Linux) instance. The issue is that when i try running my code that only connects to the server i'm getting this error:
.....node_modules/signalr/jquery.signalR.js:1085
}(window.jQuery, window));
^
ReferenceError: window is not defined
What kind of window? I have an ssh-only linux server? My existing JS code is this:
const signalR = require("signalr"); //here i can't use #aspnet/signalr or #microsoft/signalr because those aren't compatible with SignalR that's on the 3rd party server. UGH Microsoft!
let connection = new signalR.HubConnectionBuilder().withUrl("https://myurl/signalr/hubName")
.configureLogging("warn")
.build();
connection.start().then(() => {
console.log("yeeey!");
}).catch((e) => {
console.log(e);
});
What are my options here in order to run this? I have installed NodeJS on ec2 instance using official Amazon tutorial so it should be the latest current version - 13.2
Found a solution.... This library solved my problems
Please write this:
import * as signalR from '#aspnet/signalr';
we are using selenium on python and as a part of our automation, we need to capture the messages that a sample website sends and receives after the web page loaded completely.
I have check here and it is stated that what we want to do is achievable using BrowserMobProxy but after testing that, the websocket connection did not work on website and certificate errors were also cumbersome.
In another post here it is stated that, this can be done using loggingPrefs of Chrome but it seemed that we only get the logs up to the time when website loads and not the data after that.
Is it possible to capture websocket traffic using only selenium?
Turned out that it can be done using pyppeteer; In the following code, all the live websocket traffic of a sample website is being captured:
import asyncio
from pyppeteer import launch
async def main():
browser = await launch(
headless=True,
args=['--no-sandbox'],
autoClose=False
)
page = await browser.newPage()
await page.goto('https://www.tradingview.com/symbols/BTCUSD/')
cdp = await page.target.createCDPSession()
await cdp.send('Network.enable')
await cdp.send('Page.enable')
def printResponse(response):
print(response)
cdp.on('Network.webSocketFrameReceived', printResponse) # Calls printResponse when a websocket is received
cdp.on('Network.webSocketFrameSent', printResponse) # Calls printResponse when a websocket is sent
await asyncio.sleep(100)
asyncio.get_event_loop().run_until_complete(main())
My working setup at the moment:
I have an app which consists of a python flask-socketio server and a react interface. The python server collects some data from a specific connected hardware and sends it through sockets to the react interface.
What i want to add:
Besides the actual functionalities, I also want to add a communication through sockets between the local python server and a remote node server. It is possible for local python server to act as a local server and a remote client?
What i have tried:
I tried to add code for a socketio client on the main script of python server.
here:
from flask_socketio import SocketIO
import socketio
socketio_server = SocketIO(app, async_mode='gevent', engineio_logger=False)
####PART OF THE CODE THAT I TRIED TO ADD THE COMMUNICATION AS A CLIENT###
##Connecting to server as a client
sio = socketio.Client() # !!! at this point i receive a new connection on remote server, however the code freezes on this line and the script hang on until the end of the connection
sio = socketio.Connect(host, port)
#i want to send data to a specific namespace
sio.emit('remote-client', {'foo':'bar'})
########################################################################
# Serve React App
#app.route('/', defaults={'path': ''})
#app.route('/<path:path>')
def serve(path):
if path != "" and os.path.exists("static" + path):
return send_from_directory('static', path)
else:
return send_from_directory('static', 'index.html')
#---------------------Web Sockets-----------------------------#
#socketio_server.on('connect', namespace='/test')
def test_connect():
print 'Client Connected'
#socketio_server.on('disconnect', namespace='/test')
def test_disconnect():
print 'Client disconnected'
if __name__ == '__main__':
print "Starting Server..."
socketio_server.run(app, debug=False)
os.kill(os.getpid(), signal.SIGTERM)
So, is this the right approach to achieve what I want? If it is, how can the python script act as a socketio server and client at the same time?
Regards,
Ricardo
EDIT: I Just realized that I am able to use ordinary sockets (import socket) along with socketio server. But if you have a better approach I will consider!