Python Forum
Socket: does not pass when no received data.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Socket: does not pass when no received data.
#1
Hello, I am trying to make code with the socket, My goal is keep going looping without to wait for input form the socket. here my code:
import socket

v = 0

def readnet():
    if conn:
        r = conn.recv(1024)
        if not r:
            pass
        else:
            y = r.decode("utf-8")
            print("DEF READ: ", end='')
            print(y)
    if not conn:
        print("disconnected")
        return
    return (str(y))

def writenet(x):
    if conn:
        conn.sendall(x.encode("utf-8"))
        print("DEF WRITE: ", end='')
        print(x)
    if not conn:
        print("disconnected")
        return
    return

s=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(("192.168.1.200",1234))
s.listen()
conn, addr = s.accept()
s.setblocking(0)
print(f"Connected by {addr} has been established!")
blank=readnet()
writenet("HANDSHAKED")

while conn:
    v=v+1
    print(v)
    blank=readnet()    

print("disconnected")
conn.close()
print("FINISHED")
You see with 'v' value and print too, I want it keep numbers up while it's looping but the "readnet()" stuck and wait for input.
any make it pass when no data?
thanks!
Reply
#2
Set the socket to non blocking
Reply
#3
Ah, I figured out and finally worked. here is
import socket
import select
import time

v = 0

def readnet():
    if conn:
        try:
            r = conn.recv(1024)
        except TimeoutError:
            return ""
        if not r:
            return
        else:
            y = r.decode("utf-8")
            print("DEF READ: ", end='')
            print(y)
    if not conn:
        print("disconnected")
        return
    return (str(y))

def writenet(x):
    if conn:
        conn.sendall(x.encode("utf-8"))
        print("DEF WRITE: ", end='')
        print(x)
    if not conn:
        print("disconnected")
        return
    return

s=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(("192.168.1.200",1234))
s.listen()
conn, addr = s.accept()
s.setblocking(0)
conn.settimeout(0.1)
print(f"Connected by {addr} has been established!")
time.sleep(1)
blank=readnet()
writenet("HANDSHAKED")

while conn:
    v=v+1
    blank=readnet()
    if blank is None:
        print("loop ended")
        break
    if v == 10:
        writenet("read")
    if v == 20:
        writenet("write")
        v=0

print("disconnected")
conn.close()
print("FINISHED")
thanks anyway
Reply
#4
(Feb-07-2026, 03:50 AM)deanhystad Wrote: Set the socket to non blocking

Yeah see #33 at above code.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to pass encrypted pass to pyodbc script tester_V 0 2,399 Jul-27-2023, 12:40 AM
Last Post: tester_V
Question How to understand the received bytes of ser.read? pf2022 3 4,419 Mar-24-2022, 11:37 AM
Last Post: pf2022
  Help Sending Socket Data snippyro 0 1,766 Sep-23-2021, 01:52 AM
Last Post: snippyro
  Attribute Error received not understood (Please Help) crocolicious 5 4,862 Jun-19-2021, 08:45 PM
Last Post: crocolicious
  Pass by object reference when does it behave like pass by value or reference? mczarnek 2 4,111 Sep-07-2020, 08:02 AM
Last Post: perfringo
  First Byte of a string is missing while receiving data over TCP Socket shahrukh1987 3 6,669 Nov-20-2019, 10:34 AM
Last Post: shahrukh1987
  Pass by reference vs Pass by value leodavinci1990 1 3,300 Nov-20-2019, 02:05 AM
Last Post: jefsummers
  Invalid JSON payload received. Unknown name “”: Root element must be a message." hellraiser 4 18,619 Aug-18-2019, 03:41 PM
Last Post: hellraiser
  How to continue in loop until correct input received sunnyarora 10 13,343 May-04-2019, 02:37 PM
Last Post: Yoriz
  assign the variable to every value received serial from Arduino barry76 4 5,185 Feb-01-2019, 10:19 AM
Last Post: barry76

Forum Jump:

User Panel Messages

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