Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Socket reading script
#1
Hello all,

I am writing a script to know the status of a networked device. A small API allow minimum integration.
My script is basically opening a socket and buffering the data from this socket in a while loop.
It seems that each time the loop is going on, I read the same data via s.recv(1024) and therefore duplicate my output.

Is there another method to avoid this?

Extract of my code
import socket
import time

def run(self):
        def timer(host,T,Fmax,ms1):
            ms1=self.ms1
            s.settimeout(T)
            F=1
            while True: #Ctrl+C to interrupt the loop
                time.sleep(0.5)
                try:
                    data=s.recv(1024)
                    data=data.decode()
                    data=data.strip()
                except socket.timeout:
                    F+=1
                    if F==Fmax or F>Fmax:
                        break
output is like :
Quote:Status 1
Status 1
Status 1
Status 1
Status 2
Status 2
Status 3

where the device is only changing status 3 times, I should have :
Quote:Status 1
Status 2
Status 3

Any idea of what socket module I should use to avoid that? Alternatively, is there any better way to do this?
Reply


Messages In This Thread
Socket reading script - by Will86 - Apr-17-2019, 05:31 AM
RE: Socket reading script - by Gribouillis - Apr-17-2019, 06:19 AM
RE: Socket reading script - by Will86 - Apr-17-2019, 06:28 AM
RE: Socket reading script - by Gribouillis - Apr-17-2019, 06:44 AM
RE: Socket reading script - by Will86 - Apr-17-2019, 06:57 AM

Forum Jump:

User Panel Messages

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