Python Forum
How to combine data taken from server between client and GUI?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to combine data taken from server between client and GUI?
#2
If you want to keep client.py in a separate module, you need to provide some sort of interface to the data.
You can do this by wrapping your code in a function, and returning data, or by making client.py a class and exposing data
for outside use in the __init__ method, by defining data as self.data.

as a class:
Client.py
import socket

class Client:
    def __init__(self):
        self.HOST = '192.168.1.59'  # The server's hostname or IP address, NodeMCU IP Address
	    self.PORT = 80              # The port used by the server, PORT NUMBER in ARDUINO CODE

    def transfer_data(self):
        s.connect((HOST, PORT))
        s.sendall(b'Hello, world')
        self.data = s.recv(1024)

    
# To try run stand-alone
if __name__ == '__main__':
    cl = Client()
    cl.transfer_data()
    print(cl.data)
as a function:
import socket
HOST = '192.168.1.59'  # The server's hostname or IP address, NodeMCU IP Address
PORT = 80              # The port used by the server, PORT NUMBER in ARDUINO CODE 
 
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
    s.connect((HOST, PORT))
    s.sendall(b'Hello, world')
    return s.recv(1024)
Now in the GUI class:
import Client

# and add to GUI.py
class Ui_MainWindow(object):
    def __init__(self):
        self.client = Client.Client()
        self.cdata = self.client.transfer_data()
# then when needed:
        data = self.client.data()
A better way would to have a main module that imports both GUI and Client
and interfaces externally.
Reply


Messages In This Thread
RE: How to combine data taken from server between client and GUI? - by Larz60+ - Apr-01-2019, 09:10 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Paramiko Server -- Exception (server): Error reading SSH protocol banner ujlain 3 4,608 Jul-24-2023, 06:52 AM
Last Post: Gribouillis
  trying to stream data from a websocket to client tomtom 2 2,062 Jan-12-2023, 03:01 PM
Last Post: Vadanane
  Client/Server proper finalizing transfer wolfman5874 1 1,469 Jul-04-2022, 07:35 PM
Last Post: wolfman5874
Bug Problem connecting TLS client written in C++ and Twisted server gpropf 0 1,401 Jun-12-2022, 05:57 PM
Last Post: gpropf
  Server/client basic communication ebolisa 0 2,036 Sep-30-2021, 12:22 PM
Last Post: ebolisa
  Client server Multithreading Anan 6 5,919 Apr-21-2021, 08:19 PM
Last Post: SheeppOSU
Question Trouble with Client/Server reverse Shell! Gilush 0 2,796 Feb-03-2021, 01:04 PM
Last Post: Gilush
  Basic client server code question swisscheese 4 3,262 Dec-12-2020, 08:51 AM
Last Post: Larz60+
  How can i create a server for already existing client using Python? Chapanson 21 7,540 Aug-19-2020, 09:12 AM
Last Post: DeaD_EyE
  Simple TCP Client and TCP Server Problem Vapulabis 5 4,415 Jul-12-2020, 05:09 PM
Last Post: ndc85430

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020