How to flush network buffers in Apple / Python

Featured Imgs 23

I have posted this question elsewhere and gotten zero helpful responses.

Specifically on an Apple, with Python 3.9.10, I am using the socket sendall method to send a data buffer on a network connection that has successfully opened to a server. The data that I am sending does not reach the server until I use the socket's close method when I give up on waiting for data to be returned.

The intent is for the Python script to act as a network client to contact a server and do some minimal data transactions. The server is running as a service on a Linux machine. The client-side script spawns a thread to do the network communication with

import threading, sys

t = threading.Thread(target=social_init, daemon=True)
t.start()

The client-side spawned code in question (in part) is:

import socket               # Import socket module and JSON parser module
import json
def social_init():
    global social1_resps, social2_resps, social3_resps
    host = "test.mydomain.com"  # Get server name
    port = 6942                 # this is where the server is listening
    with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
        s.settimeout(30.0)    # note: 30 second timeout
        s.connect((host, port))
        sbuf = json.dumps({"rmv":"1","gameID":9,"function":1})
        bbuf = sbuf.encode('utf-8')
        nbytes = len(bbuf)
        s.sendall(nbytes.to_bytes(4, 'little'))
        s.sendall(bbuf)

        nnbuf = s.recv(4)
        nbytes = int.from_bytes(nnbuf, 'little')
        bbuf = s.recv(nbytes)
        sbuf = str(bbuf, 'utf-8')
        obj = json.loads(sbuf)
        while (obj != None):
            # actual processing of returned data elided

        s.close()                   # Close the socket when done

This code works as expected on Windows and Linux hosts, but on an Apple host the server sees the connection happen, but no data. When the client times out the receive, or when the script terminates (which forces the connection closed), we see the data being sent - I've checked timing by running Wireshark on the server.

I'm aware of the existence of the flush() function, but because we're using sockets rather than the file handle abstraction, flush apparently does not apply. I have tried the file handle abstraction (socket.makefile) with no better success - in that case flush apparently flushes the file object buffers to the network subsystem, but it still does not hit the wire. Is there an equivalent method that can be used on a socket to force flushing of the network buffer?