So right now I have rpyc client A that scrapes data and when it finds specific data it sends it to my rpyc server which is then stored. Lets say that I also have rpyc client B, C and D connected to my rpyc server as well. How can I make it so that the server can send that data directly to client C?
Since rpyc is symmetric, clients can pass callbacks for the server to call. The callbacks then get executed in the client process. That's probably the simplest and cleanest way to do what you want.
So your server needs to expose a new register_callback(client_callback) method, which stores the callbacks in a list. Then, whenever new data is received, you simply invoke all stored callbacks.
You'd also need to handle the case where a client which had registed a callback has already disconnected. It should be as simple as adding the correct try/except around the callback call (though I don't remember what's the actual exception type you'd need to catch).
Related
I'm currently making a guessing game in Python and I'm trying to use select.select to allow multiple clients to connect to my server but I cannot wrap my head around how to use select.select. I've look all over the internet but all the tutorials I've come across are for chat servers which I can't seem to relate to.
I was just wondering how I'd let multiple clients connect to my server through select.select. And also how would I send/receive data to/from individual clients using select.select
I've look all over the internet but all the tutorials I've come across
are for chat servers which I can't seem to relate to.
There's no difference between a chat server and game server regarding the use of select.select.
I was just wondering how I'd let multiple clients connect to my server
through select.select.
You'd pass the server socket (which you called listen on) in the rlist argument to select; if after return from select the server socket is in the first list (the objects that are ready for reading) of the returned triple of lists, you'd call accept on the server socket and thus get the new client socket, which you'd append to the rlist in subsequent select calls.
And also how would I send/receive data to/from individual clients
using select.select
If after return from select a client socket is in the first list (the objects that are ready for reading) of the returned triple of lists, you'd receive data by calling recv on that client socket.
You don't need to use select for writing; you'd just send data by calling send.
See the question "Handle multiple requests with select" for an example server.
I am trying to implement a multi-threading server which can handle with simultaneously read/write from client.
The server method:
The client connects to the server, when each message starts with the name of the user they want to send the message to, followed by '|'. It looks something like that: "USER_NAME|DATA".
After receiving the data, the server knows by a dictionary of {socket:username} where to send the data. Everything works great, except the fact that the client can't handle with simultaneously reading and writing. I searched for a method to handle that and i found the select() function, but with a lack of examples- i couldn't integrate that function in my code.
therefore I have 2 questions:
Is the select() function should be on the server side? will it be more efficient?
Is someone can demonstrate with a simple example how the select() method should look in the client side?
Thanks in advance!!!
Though select() will work, you have to use threads if you want to do other things while the system is blocked on the select.
Have a look at glib's GIO library. There you can connect callbacks to the actions you want to monitor or act on, for example the 'connect's from clients.
Just open a socket, and use its file descriptor to hang a gio.add_watch on. Here's a mini-tutorial on using giochannels.
I'm writing a python server/client app. If I serialize a function on the client and pass it to the server, can the server use it as a callback? I'm assuming there must be something extra I'd have to do as the client and server are communicating via packets, I just don't know what.
What I actually need is for the server to change one of the client's attributes (when the server is ready to accept another command), and I want an alternative to having the client continuously poll the server. Thanks for any help.
Take a look at Twisted JSON RPC.
A recent SO post: Python Twisted JSON RPC
I need a way to simulate connectivity problems in an automated test suite, on Linux, and preferably from Python. Some sort of proxy that I can put in front of the web server that can hang or drop connections after one trigger or another (after X bytes transferred, etc) would be perfect.
It doesn't seem too hard to build, but I'd rather grab something pre-existing, if anyone has any good recommendations.
when i needed one, i found that building it yourself is the best thing..
start by raising a threaded server in python http://docs.python.org/dev/library/socketserver.html (you don't have to use the class itself).
and it's very simple:
in the new connection thread, you create a new socket and connects it to the real server.
then, you put both of them in a list and sends it to select.select (import select).
then, when socket x receive data - sends it to y. when socket y receives data sends it to x. (don't forget to close the socket when you receive empty string).
now you can do whatever you want..
if you need anything, i'm here..
I have a simple TCP client which is connected to twisted using:
reactor.connectTCP(host, port, SomeClientFactory())
The program is able to receive a HUP signal to trigger a reload. I'd like to basically:
Remove the old clients
Reload config
Create new clients based upon new config
However, I can't seem to find a way to acheive the first of these points. Any tips?
Thanks
IReactorTCP.connectTCP returns an IConnector provider. As you can see on the definition of the IConnector interface, the disconnect method will do something like what you want. You can also use the protocol instance's transport attribute's loseConnection method, of course. The latter would be more suitable if there's any kind of cleanup you want the protocol to do before actually disconnecting, since you could put that work and a call to loseConnection at the end of a method like shutdown or quit or cleanup on the protocol class and then just call that.