Python Forum
Looping sent message with socket
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Looping sent message with socket
#2
This makes no sense. s is a socket, s.send is a function. But after you run this method s is a string. No more sending anything over that socket.
    def clickpolacz(self):
        TCP_IP = self.adres.toPlainText()
        TCP_PORT = 8000
        s.connect((TCP_IP, TCP_PORT))
        self.wyslana.setText('Połączono')
        s.send = '[000000]'
And speaking of s, that is a terrible variable name. I would call it "mysocket", or call it "socket" and make it an attribute of Ui_MainWindow instead of a global variable.

Speaking of global variables, there is no need for the global keyword here:
from PyQt5 import QtCore, QtGui, QtWidgets
import socket
import time
global s
The global keyword tells python to "look for this variable in the global namespace, do not make a local variable". It makes sense to use this inside of a function.
def create_socket():
    global s
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
Here the global keyword tells python to use the "s" variable defined in the global scope. If there is no "s" defined in the global scope one is created. When you use "global s" outside a function it makes no sense. You are already in the global scope. Any variable defined here is global by default.

You cannot do any of this:
        while True:
            self.send = '[' + '0' + str(led1 + led2) + str(slide1) + str(slide2) + ']'
            self.wyslana.setText(self.send)
            s.send(str.encode(str(self.send)))
            self.send(str.encode(str(self.send)))
            time.sleep(0.5)
First off, you can only have one "forever" loop in a Qt application, and that forever loop is inside app.exec_(). If your program needs to do something periodically (every half second) you have to do that in a separate thread, or use a QTimer event.

Another problem is that you are treating self.send as a function right after you assign it the to reference a string. This makes self.send a str.
self.send = '[' + '0' + str(led1 + led2) + str(slide1) + str(slide2) + ']'
Now that it is a str you cannot call it like a function.
self.send(str.encode(str(self.send)))
Reply


Messages In This Thread
Looping sent message with socket - by anastejzja - Jun-16-2022, 06:59 PM
RE: Looping sent message with socket - by deanhystad - Jun-16-2022, 09:51 PM

Forum Jump:

User Panel Messages

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