1

I have (what is likely) a very beginner-type question about sockets in Python 3 that I haven't been able to find an answer to so far:

Excluding options like threading or concurrency, what I'd like to know is, is it possible to send something like a string from a client.py to a server.py, but then have the server module perform different actions with that string depending on which method from the client sent it? And how?

So, for example, in one case the user inputs a string in client.py, which sends it to server.py to perform the task of matching and deleting it from a list, but then in another case, the server returns True if that string is found in the list.

NuclearFish
  • 69
  • 1
  • 4
  • Essentially you are able to do whatever you want with sockets by creating an API that will satisfy your needs. You would have to encode the information about the call into the data sent. – shuttle87 Mar 11 '18 at 05:21
  • Is there something more specific that I should be aware of? I'm not sure what to use to tell the `server.py` to do a specific task on a sent variable. – NuclearFish Mar 11 '18 at 05:42
  • This question is ridiculously broad. I'm going to put an answer in, to point you in the right direction, but you should have a code example with a _specific problem you're trying to solve_ in the future. This question belongs closed. – g.d.d.c Mar 11 '18 at 06:33

1 Answers1

0

Presuming you have something like client.py:

# sock is a connected socket
sock.send(json.dumps({'command': 'RemoveItem', 'item': 'SomeItem}))

Then in server.py you might have:

while True:
  data = json.loads(sock.read(100))
  if data['command'] == 'RemoveItem':
    # Code here that removes from whatever list server.py is mantaining.

But, the number of possible implementations is literally unbounded. This example uses JSON to serialize commands. There are any number of methods you might use to make this work.

g.d.d.c
  • 46,865
  • 9
  • 101
  • 111