Found a piece of code.
Actually, everything works, the server and the client are having fun communicating, but all this is terribly inconvenient. Help wrap it all in one class.

That is, there must be 3 functions inside the class:

  1. Socket connection
  2. Receive server response
  3. Sending data, receiving a response to a request

It should go like this: connect to the server (we receive an invitation from it), enter data, in response we receive a message corresponding to the entered data.

This piece of code is from a telnet client on a socket.
Maybe I'm trying to get something wrong.
I am trying to bind events to gui objects, respectively, functions, not procedures, are needed:

# !python import socket, select, string, sys host = '0.0.0.0' port = 8080 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.settimeout(2) # connect to remote host try : s.connect((host, port)) except : print 'Unable to connect' sys.exit() print 'Connected to remote host' while 1: socket_list = [sys.stdin, s] # Get the list sockets which are readable read_sockets, write_sockets, error_sockets = select.select(socket_list , [], []) for sock in read_sockets: #incoming message from remote server if sock == s: data = sock.recv(4096) if not data : print 'Connection closed' sys.exit() else : #print data sys.stdout.write(data) #user entered a message else : msg = sys.stdin.readline() s.send(msg) 

Closed due to the fact that the essence of the issue is incomprehensible to the participants of jfs , Xander , αλεχολυτ , mymedia , Lex Hobbit Aug 14 '17 at 1:34 .

Try to write more detailed questions. To get an answer, explain what exactly you see the problem, how to reproduce it, what you want to get as a result, etc. Give an example that clearly demonstrates the problem. If the question can be reformulated according to the rules set out in the certificate , edit it .

  • What is the question? You do not know how to create a class in Python? (if you don’t know, ask a separate question, with a minimal example of code that you yourself wrote) What do you want to achieve by creating a class here? What does "uncomfortable" mean? Give a specific example of use (as now and as you want - before and after the code) - also describe in words what the example does. On a trifle: there are no procedures in Python, there are only functions, and what you call functions are usually called methods, which are also implemented as Python in function. - jfs
  • dear Aid, however, useless dinosaur, understood the essence of my question) - Smile
  • the question did not become clearer. If you want to execute the GUI code in the same process (which usually starts a certain event loop), then, given the simplicity of the server code, you can select delegate to the GUI. For example, tkinter has createfilehandler() , gtk has io_add_watch () . To support console I / O support, streams can be used for the same task . (in examples s / proc.stdout.read / sys.stdin.read /) - jfs

1 answer 1

In the current implementation it will be very inconvenient to simply put the code in a separate class, the reason for this is while 1 . I would advise you to start by first putting all the blocks inside if \ else into separate functions, giving them all clear names, and when you reach the endless while, you will have to look towards python threading .

The result should be a class that creates a stream, and calls methods in the stream to receive fresh messages and send its own.