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:
- Socket connection
- Receive server response
- 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)
selectdelegate to the GUI. For example, tkinter hascreatefilehandler(), 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