sockets - Use of select() in a python client -
i'm building python client project. know little things python .. sorry if code isn't great ! client works fine, connects server, , "talks" him , easy parts. want use select in c. client looks :
p = getoptions() mess = messageclass() ic = interpretclass() cc = commandclass() s = none def send_name_to_server(s): var = 'team ' var += p.getname() #var += '\r\n' mess.sendmessage(s, var) def protocol(s): rec = mess.readmessage(s) if (ic.interpret_bienvenue(s, rec, p) == 1): send_name_to_server(s) rec = mess.readmessage(s) ic.interpret_num_client(s, rec, p) rec = mess.readmessage(s) ic.interpret_size(s, rec, p) var = 'ok' mess.sendmessage(s, var) def main(): try: p.parseopt() if not p.getname(): print('exception : need name start') sys.exit() mc = moduleconnect() s = mc.connect(p.gethost(), p.getport()) protocol(s) s.close() except connectionrefusederror: print('exception : server has refused connection') except getopt.getopterror: print('usage : client.py -n [name] -h [host] -p [port]') except: print('exception : error has occured') if __name__ == "__main__": main()
and class moduleconnect looks :
import socket
class moduleconnect(object): """ create connexion """ def connect(self, host, port): s = socket.socket() s.connect((host, int(port))) return s
so how works ? parse arguments received command line parseopt method (not important here) init connection method connect moduleconnect , talk server protocol method. simple think ? in c in order use select :
fd_set(0, &(c->read_fds)); fd_set(c->sd, &(c->read_fds)); if (select(c->sd + 1, &(c->read_fds), null, null, null) == -1) { perror("client-select() error ! "); exit(1); } if (fd_isset(c->sd, &(c->read_fds))) read_message_from_server(c); if (fd_isset(0, &(c->read_fds))) send_message(c);
if server talks me first read told me, if not send message got send him. cannot find way in python ..
again sorry, i'm total noob python , , following tutorial can't find way.
thanks !
in order avoid problem kept reading , sending (almost) @ same time, , when read souldn't threw away. no use of select, , juste read/write.
Comments
Post a Comment