Python efficient socket communication -
i started making pure skype resolver , after doing fine stuck on socket communication.
let me explain
i'm using python user's ip , script opens socket server , sends username other program written in .net
why that? well, python skype api not powerfull i'm using axskype library in order gather more info.
the problem
the python socket sends username should dont know efficient way info back. thinking opening socket server in same script , wait .net program sends back.
i dont kwon how make fast possible i'm asking help.
the code
class api: def get(self, username): skypeapi.activateskype(username) time.sleep(1) # because skype ew buf = [] print("==========================") print("resolving user " + username) #this i'm starting socket , sending data s = socket.socket(socket.af_inet, socket.sock_stream) s.connect(("127.0.0.1", 5756)) s.sendall(username) s.close() #at poaint want data .net app logfile in glob.glob('*.log'): buf += logparse.search(logfile, username) print("done!") print("==========================") return json.dumps(buf) class index: def get(self): return render.index() if __name__ == "__main__": app.run()
you can bind socket connection. way, socket stream remain open , able send , receive information easily. integrate _thread
module , able handle multiple streams. here example code binds socket stream , sends whatever clients sends it(although in case send whatever data necessary)
import socket _thread import * #clienthandle function receive , send stuff specific client. def clienthandle(stream): stream.send(str.encode("enter stuff: ")) while true: #here program waits response. 4000 buffer limit. data = stream.recv(4000) if not data: #if there not data, exit loop. break stream.senddall(str.encode(data + "\n")) #creating socket. s = socket.socket(socket.af_inet, socket.sock_stream) host = "" #in case host localhost can put host port = 80 try: #here program tries bind socket stream. s.bind((host, port)) except socket.error e: print("there error: " + str(e)) #main program loop. uses multithreading handle multiple clients. while true: conn, addr = s.accept() print("connected to: " + addr[0] + ": " + str(addr[1])) start_new_thread(clienthandle,(conn,))
now in case, can integrate api
class(is want integrate it? correct me if i'm wrong.). when define , bind socket, use code:
s = socket.socket(socket.af_inet, socket.sock_stream) s.bind((host, port))
where, in case, host
127.0.0.1
, in other words, localhost, can accessed socket.gethostbyname(socket.gethostname())
(but that's bit verbose), , port
, 5756
. once have bounded socket, have accept connections through following syntax:
conn, addr = s.accept()
which can pass conn
, addr
whatever function or use in other code.
regardless of use in, receive data can use socket.recv()
, pass buffer limit. (remember decode whatever receive.) , of course, send data using socket.sendall()
.
if combine _thread
module, shown above, can handle multiple api requests, come handy in future.
hope helps.
Comments
Post a Comment