Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Socket as context manager only works with Python 3
#1
Using RoboDK 4.0.1 on Mac...
robolink.py:922

I have to use Python2.7 as I'm calling RoboDK API from Maya, but `with socket` doesn't work with Python 2.7.

I changed to the code below and it seems to work. 
Code:
        elif ('/NEWINSTANCE' in self.ARGUMENTS or '-NEWINSTANCE' in self.ARGUMENTS):
            from socket import socket
            if sys.version_info.major == 3:
                with socket() as s:
                    s.bind(('',0))
                    port = s.getsockname()[1]
                    print("Using available port %i" % port)
                    self.PORT_START = port
                    self.PORT_END = port
                    self.ARGUMENTS.append("-PORT=%i" % port)
            else:
                sock = socket()
                sock.bind(('',0))
                port = sock.getsockname()[1]
                print("Using available port %i" % port)
                self.PORT_START = port
                self.PORT_END = port
                self.ARGUMENTS.append("-PORT=%i" % port)
                sock.close()
#2
Thank you for the tip to make the RoboDK API support Python 2! 

We just updated it on PyPi and GitHub.
  




Users browsing this thread:
1 Guest(s)