Skip to content Skip to sidebar Skip to footer

Server-client Connection In Pyqt

I would like to write a simpe program that can handle a server-client connection (1:1 connection is enough) in PyQt. I was able to write the socket part but with the GUI I have no

Solution 1:

To keep a few things simple:

The resultant of your question is spending a lot of coding time (approx. 2-3 hours). Thus a tiny walk-through to get you on the right path again to do the next few steps yourself.

  1. Your main program file in this case is Dialog.py and not Client.py.
  2. If you build/compile your program Client.py is either client.pyd or another form of library. On windows Dialog will be an executable.
  3. rename Client.py into LOGIN_GUI or something that refers to its function of "local login" because that is what you try to accomplish. Use name client.py for remote logging (different GUI than server!). 3.In Dialog use pythons multiprocessing.

    3a. Process 1 runs main app;

    3b. Process 2 runs client-server;

    3c. Outsource other tasks taking more than 1-2sec (shows as "freezing" in main GUI).

  4. Dialog.py => MyMainAppNameisNOtDialog.py (avoid confusion/typo's inside your scripting code)

If you code your server ThreadingMixIn style like below you are able to log-in from two different computers. This is as far as I know not available as tool in pyqt5. See also inline comments inside the below code.

Server-side (modification of pythons Socket-server ThreadingMixIn):

import socket
import threading
import SocketServer

# MyAwesomelyNamedServerScript.pyclassThreadedTCPRequestHandler(SocketServer.BaseRequestHandler):

    defhandle(self):

        deffunc1(scr1):
            #put code that does something here.print'func1 : %s' % scr1
            return scr1

        deffunct2(scr2):
            #put code that does something here.print'func2 : %s' % scr2
            return scr2


        # self.request is the TCP socket connected to the client
        cur_thread = threading.current_thread()
        data = self.request.recv(1024)

        # In the data package the IP from client. # Extract and add to an IP list (e.g. max 2 connection.)# if/else statement: if max connection reached than report back "cannot connect... waiting in queue or something".# limiting the request also prevent DDos attacks from the start.

        data_proc = func1(data)       # do something once
        data      = func2(data_proc)  # do something twice

        response = "{}: {}".format(cur_thread.name, data)
        self.request.sendall(response)

classThreadedTCPServer(SocketServer.ThreadingMixIn, SocketServer.TCPServer):
    passdefsetupServer(ip = None, port = None, message = None):
    # Ip, port, message can be linked to below code if you want exernal input via GUI# Port 0 means to select an arbitrary unused port
    HOST, PORT = "localhost", 0

    server = ThreadedTCPServer((HOST, PORT), ThreadedTCPRequestHandler)
    ip, port = server.server_address

    # Start a thread with the server -- that thread will then start one# more thread for each request
    server_thread = threading.Thread(target=server.serve_forever)
    # Exit the server thread when the main thread terminates
    server_thread.daemon = True
    server_thread.start()
    print"Server loop running in thread:", server_thread.name
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sock.connect((ip, port))
    try:
        sock.sendall(message)
        response = sock.recv(1024)
        print"Received: {}".format(response)
    finally:
        sock.close()

deftest_main():

        #client(ip, port, "Hello World 1")#client(ip, port, "Hello World 2")#client(ip, port, "Hello World 3")

        client(message = "Hello World 1")
        client(message = "Hello World 2")
        client(message = "Hello World 3")

        server.shutdown()
        server.server_close()

if __name__ == "__main__":

    test_main()  #runs the main test

The MainApp.py script in short:

import myloginscript
import myAwesomeMainGUI

...snippet ... <incl. Qt imports, etc.> ...

#MainApp.pyclassMyMainUI(QtWidgets.QMainWindow, myAwesomeMainGUI.Ui_MainWindow):

    ...snippet <your code>...

    defsetUpass(self, username):
        # do whatever you want with the username# if Pword brought here then login.username.text() needs to be altered to get pw instead.
        self.username = username  # this variable shoud be made inside your main code.#        self.label.setText("Username: %s" % self.username)print'The username is : %s' % self.username

if _name__ = '__main__':

    app.QApplication(sys.argv)

    login= myloginscript()  # this runs your login script (GUI starts if attached therein.)if login.exec():
        window = MymainwindowDialog()           # main appwindow...not login GUI.
        window.setUpass(login.username.text())
        window.show()
        sys.stdout.flush()
        try:
            sys.exit(app.exec())
        except:
            print('Exit now.. wrong Uname or Pword')
    else:
        sys.exit(-1)  # user pressed 'Cancel', lets quit the program here.

Post a Comment for "Server-client Connection In Pyqt"