Skip to main content
refine the code
Source Link
AsukaMinato
  • 10.4k
  • 1
  • 17
  • 44
import socketserver
import json
import time


class TCPHandler(socketserver.BaseRequestHandler):
    def handle(self):
        bufSz = 2048
        while(True):
            self.data = self.request.recv(bufSz)
            if self.data:
                jsonReq = json.loads(self.data.strip())
                print(f"Request: {jsonReq}")
                jsonRes = json.dumps({"res": len(jsonReq["arg"])})
                print(f"Response: {jsonRes}")
                self.request.sendall(bytes(jsonRes, "utf-8"))
            else:
                time.sleep(0.10)
                self.data = ""
                continue


def main():
    host, port = "localhost", 9990
    with socketserver.TCPServer((host, port), TCPHandler) as server:
        print(f"Test server now running at {host} on port {port}")
        server.serve_forever()


if __name__ == "__main__":
    main()
import socketserver
import json
import time


class TCPHandler(socketserver.BaseRequestHandler):
    def handle(self):
        bufSz = 2048
        while True:
            self.data = self.request.recv(bufSz)
            if self.data:
                jsonReq = json.loads(self.data.strip())
                print(f"Request: {jsonReq}")
                jsonRes = json.dumps({"res": len(jsonReq["arg"])})
                print(f"Response: {jsonRes}")
                self.request.sendall(bytes(jsonRes, "utf-8"))
            else:
                time.sleep(0.10)
                self.data = ""
                continue


def main():
    host, port = "localhost", 9990
    with socketserver.TCPServer((host, port), TCPHandler) as server:
        print(f"Test server now running at {host} on port {port}")
        server.serve_forever()


if __name__ == "__main__":
    main()
import socketserver
import json
import time


class TCPHandler(socketserver.BaseRequestHandler):
    def handle(self):
        bufSz = 2048
        while(True):
            self.data = self.request.recv(bufSz)
            if self.data:
                jsonReq = json.loads(self.data.strip())
                print(f"Request: {jsonReq}")
                jsonRes = json.dumps({"res": len(jsonReq["arg"])})
                print(f"Response: {jsonRes}")
                self.request.sendall(bytes(jsonRes, "utf-8"))
            else:
                time.sleep(0.10)
                self.data = ""
                continue


def main():
    host, port = "localhost", 9990
    with socketserver.TCPServer((host, port), TCPHandler) as server:
        print(f"Test server now running at {host} on port {port}")
        server.serve_forever()


if __name__ == "__main__":
    main()
import socketserver
import json
import time


class TCPHandler(socketserver.BaseRequestHandler):
    def handle(self):
        bufSz = 2048
        while True:
            self.data = self.request.recv(bufSz)
            if self.data:
                jsonReq = json.loads(self.data.strip())
                print(f"Request: {jsonReq}")
                jsonRes = json.dumps({"res": len(jsonReq["arg"])})
                print(f"Response: {jsonRes}")
                self.request.sendall(bytes(jsonRes, "utf-8"))
            else:
                time.sleep(0.10)
                self.data = ""
                continue


def main():
    host, port = "localhost", 9990
    with socketserver.TCPServer((host, port), TCPHandler) as server:
        print(f"Test server now running at {host} on port {port}")
        server.serve_forever()


if __name__ == "__main__":
    main()
edited tags
Link
matrix42
  • 7.2k
  • 3
  • 26
  • 67
Tweeted twitter.com/StackMma/status/1395937612070891524
deleted 18 characters in body
Source Link
Shredderroy
  • 5.4k
  • 18
  • 28

I have a large codebase in which some functions make calls to Python TCP servers. The code used to work perfectly in Mathematica 12.2, but has not worked since I updated to Mathematica 12.3 last night.

I have managed to isolate the problem to the call to the invocation of WriteString. The following is a minimum illustration of the issue. Here is the Python server:

import socketserver
import json
import time


class TCPHandler(socketserver.BaseRequestHandler):
    def handle(self):
        bufSz = 2048
        while(True):
            self.data = self.request.recv(bufSz)
            if self.data:
                jsonReq = json.loads(self.data.strip())
                print(f"Request: {jsonReq}")
                jsonRes = json.dumps({"res": len(jsonReq["arg"])})
                print(f"Response: {jsonRes}")
                self.request.sendall(bytes(jsonRes, "utf-8"))
            else:
                time.sleep(0.10)
                self.data = ""
                continue


def main():
    host, port = "localhost", 9990
    with socketserver.TCPServer((host, port), TCPHandler) as server:
        print(f"Test server now running at {host} on port {port}")
        server.serve_forever()


if __name__ == "__main__":
    main()

And here is the Wolfram Language code that calls it:

ClearAll[sock];
sock = SocketConnect[{"localhost", 9990}, "TCP"];

ClearAll[params];
params = ExportString[<|"arg" -> "test string"|>, "JSON"];

Module[{res},
    WriteString[sock, params];
    res = ByteArrayToString@SocketReadMessage[sock];
    ImportString[res, "JSON"]
]

This exact set-up used to work for me reliably until Mathematica 12.2. But now the execution hangs at the WriteString command.

I have tried turning off Windows firewall entirely. The problem persists.

Any assistance would be gratefully acknowledged.

I have a large codebase in which some functions make calls to Python TCP servers. The code used to work perfectly in Mathematica 12.2, but has not worked since I updated to Mathematica 12.3 last night.

I have managed to isolate the problem to the call to the invocation of WriteString. The following is a minimum illustration of the issue. Here is the Python server:

import socketserver
import json
import time


class TCPHandler(socketserver.BaseRequestHandler):
    def handle(self):
        bufSz = 2048
        while(True):
            self.data = self.request.recv(bufSz)
            if self.data:
                jsonReq = json.loads(self.data.strip())
                print(f"Request: {jsonReq}")
                jsonRes = json.dumps({"res": len(jsonReq["arg"])})
                print(f"Response: {jsonRes}")
                self.request.sendall(bytes(jsonRes, "utf-8"))
            else:
                time.sleep(0.10)
                self.data = ""
                continue


def main():
    host, port = "localhost", 9990
    with socketserver.TCPServer((host, port), TCPHandler) as server:
        print(f"Test server now running at {host} on port {port}")
        server.serve_forever()


if __name__ == "__main__":
    main()

And here is the Wolfram Language code that calls it:

ClearAll[sock];
sock = SocketConnect[{"localhost", 9990}, "TCP"];

ClearAll[params];
params = ExportString[<|"arg" -> "test string"|>, "JSON"];

Module[{res},
    WriteString[sock, params];
    res = ByteArrayToString@SocketReadMessage[sock];
    ImportString[res, "JSON"]
]

This exact set-up used to work for me reliably until Mathematica 12.2. But now the execution hangs at the WriteString command.

I have tried turning off Windows firewall entirely. The problem persists.

Any assistance would be gratefully acknowledged.

I have a large codebase in which some functions make calls to Python TCP servers. The code used to work perfectly in Mathematica 12.2, but has not worked since I updated to Mathematica 12.3 last night.

I have managed to isolate the problem to the call to WriteString. The following is a minimum illustration of the issue. Here is the Python server:

import socketserver
import json
import time


class TCPHandler(socketserver.BaseRequestHandler):
    def handle(self):
        bufSz = 2048
        while(True):
            self.data = self.request.recv(bufSz)
            if self.data:
                jsonReq = json.loads(self.data.strip())
                print(f"Request: {jsonReq}")
                jsonRes = json.dumps({"res": len(jsonReq["arg"])})
                print(f"Response: {jsonRes}")
                self.request.sendall(bytes(jsonRes, "utf-8"))
            else:
                time.sleep(0.10)
                self.data = ""
                continue


def main():
    host, port = "localhost", 9990
    with socketserver.TCPServer((host, port), TCPHandler) as server:
        print(f"Test server now running at {host} on port {port}")
        server.serve_forever()


if __name__ == "__main__":
    main()

And here is the Wolfram Language code that calls it:

ClearAll[sock];
sock = SocketConnect[{"localhost", 9990}, "TCP"];

ClearAll[params];
params = ExportString[<|"arg" -> "test string"|>, "JSON"];

Module[{res},
    WriteString[sock, params];
    res = ByteArrayToString@SocketReadMessage[sock];
    ImportString[res, "JSON"]
]

This exact set-up used to work for me reliably until Mathematica 12.2. But now the execution hangs at the WriteString command.

I have tried turning off Windows firewall entirely. The problem persists.

Any assistance would be gratefully acknowledged.

added 18 characters in body
Source Link
Shredderroy
  • 5.4k
  • 18
  • 28
Loading
Source Link
Shredderroy
  • 5.4k
  • 18
  • 28
Loading