Efficient Cellular Python

I recently started building a clean skype resolver and after everything was fine I got stuck with socket communication.

Let me explain

I am using python to get the user's IP address and then the script opens a socket server and sends the username to another program written in .NET.

Why? Well, the skype python API is not that powerful, so I am using the axSkype library to gather more information.

Problem

The python socket is sending the username as it should, but I don't know the most efficient way to get the information back. I was thinking of opening a socket server in the same script and waiting for the .NET program to send back.

I do not know how to do this as quickly as possible, so I am asking for your help.

Code

class api:
  def GET(self, username):
    skypeapi.activateSkype(username)
    time.sleep(1) # because skype is ew
    buf = []
    print("==========================")
    print("Resolving user " + username)
    #This is where i'm starting the socket and sending data
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.connect(("127.0.0.1", 5756))
    s.sendall(username)
    s.close()
    #at this poaint i want to get data back from the .NET app
    for 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()

      

+3


source to share


1 answer


You can bind your socket to a connection. This way, your socket stream will remain open and you can send and receive information easily. Integrate this with the module _thread

and you can handle multiple threads. Here is some sample code that binds a socket to a stream and just sends back whatever the clients send it (although in your case you can send any data)

import socket
from _thread import *

#clientHandle function will just receive and send stuff back to a specific client.
def clientHandle(stream):
    stream.send(str.encode("Enter some stuff: "))
    while True:
        #Here is where the program waits for a response. The 4000 is a buffer limit.
        data = stream.recv(4000)
        if not data:
           #If there is not data, exit the loop.
           break
        stream.senddall(str.encode(data + "\n"))

        #Creating socket.
        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        host = "" #In this case the host is the localhost but you can put your host
        port = 80
        try:
            #Here the program tries to bind the socket to the stream.
            s.bind((host, port))
        except socket.error as e:
            print("There was an error: " + str(e))

        #Main program loop. Uses multithreading to handle multiple clients.
        while True:
            conn, addr = s.accept()
            print("Connected to: " + addr[0] + ": " + str(addr[1]))
            start_new_thread(clientHandle,(conn,))

      

Now in your case, you can integrate this into your class api

(is this where you want to integrate it?) Correct me if I'm wrong.). So now when you define and bind your socket use this code:

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((host, port))

      

Where in your case host

is 127.0.0.1

, in other words, your localhost, which can also be accessed socket.gethostbyname(socket.gethostname())

(but it's a little verbose), and then port

that's for you 5756

. After you have restricted your socket, you must accept connections through the following syntax:



conn, addr = s.accept()

      

That you can pass conn

, and addr

in any function or simply use any other code.

No matter what you use it for, you can use socket.recv()

and pass a buffer limit to it to get data . (Remember to decode whatever you receive.) And, of course, you send data using socket.sendall()

.

If you combine this with a module _thread

as shown above, you can handle multiple api requests that might come in handy in the future.

Hope it helps.

+1


source







All Articles