8
8
import subprocess
9
9
import multiprocessing
10
10
11
- if sys .version_info >= (3 , 0 ):
12
- ADDRESS_IN_USE_ERROR = OSError
13
- else :
14
- import socket
15
- ADDRESS_IN_USE_ERROR = socket .error
16
-
17
11
import tornado .web
18
12
import tornado .ioloop
19
13
import tornado .websocket
@@ -43,7 +37,7 @@ def start_zmq_server_as_subprocess(zmq_url=None, server_args=[]):
43
37
"""
44
38
Starts the ZMQ server as a subprocess, passing *args through popen.
45
39
Optional Keyword Arguments:
46
- zmq_url
40
+ zmq_url
47
41
"""
48
42
# Need -u for unbuffered output: https://stackoverflow.com/a/25572491
49
43
args = [sys .executable , "-u" , "-m" , "meshcat.servers.zmqserver" ]
@@ -58,16 +52,13 @@ def start_zmq_server_as_subprocess(zmq_url=None, server_args=[]):
58
52
# e.g. on Windows SYSTEMROOT and PATH
59
53
env = dict (os .environ )
60
54
env ["PYTHONPATH" ] = os .path .dirname (os .path .dirname (os .path .dirname (__file__ )))
61
- kwargs = {
62
- 'stdout' : subprocess .PIPE ,
63
- 'stderr' : subprocess .PIPE ,
64
- 'env' : env
65
- }
66
55
# Use start_new_session if it's available. Without it, in jupyter the server
67
56
# goes down when we cancel execution of any cell in the notebook.
68
- if sys .version_info .major >= 3 :
69
- kwargs ['start_new_session' ] = True
70
- server_proc = subprocess .Popen (args , ** kwargs )
57
+ server_proc = subprocess .Popen (args ,
58
+ stdout = subprocess .PIPE ,
59
+ stderr = subprocess .PIPE ,
60
+ env = env ,
61
+ start_new_session = True )
71
62
line = ""
72
63
while "zmq_url" not in line :
73
64
line = server_proc .stdout .readline ().strip ().decode ("utf-8" )
@@ -117,7 +108,7 @@ def find_available_port(func, default_port, max_attempts=MAX_ATTEMPTS, **kwargs)
117
108
port = default_port + i
118
109
try :
119
110
return func (port , ** kwargs ), port
120
- except (ADDRESS_IN_USE_ERROR , zmq .error .ZMQError ):
111
+ except (OSError , zmq .error .ZMQError ):
121
112
print ("Port: {:d} in use, trying another..." .format (port ), file = sys .stderr )
122
113
except Exception as e :
123
114
print (type (e ))
@@ -166,7 +157,7 @@ def set_extra_headers(self, path):
166
157
class ZMQWebSocketBridge (object ):
167
158
context = zmq .Context ()
168
159
169
- def __init__ (self , zmq_url = None , host = "127.0.0.1" , port = None ,
160
+ def __init__ (self , zmq_url = None , host = "127.0.0.1" , port = None ,
170
161
certfile = None , keyfile = None , ngrok_http_tunnel = False ):
171
162
self .host = host
172
163
self .websocket_pool = set ()
@@ -214,19 +205,14 @@ def f(port):
214
205
import pyngrok .conf
215
206
import pyngrok .ngrok
216
207
217
- kwargs = {}
218
208
# Use start_new_session if it's available. Without it, in
219
209
# jupyter the server goes down when we cancel execution of any
220
210
# cell in the notebook.
221
- if sys .version_info .major >= 3 :
222
- kwargs ['start_new_session' ] = True
223
- config = pyngrok .conf .PyngrokConfig (** kwargs )
211
+ config = pyngrok .conf .PyngrokConfig (start_new_session = True )
224
212
self .web_url = pyngrok .ngrok .connect (self .fileserver_port , "http" , pyngrok_config = config )
225
213
226
214
# pyngrok >= 5.0.0 returns an NgrokTunnel object instead of the string.
227
- if sys .version_info .major < 3 :
228
- self .web_url = self .web_url .decode ("utf-8" )
229
- elif not isinstance (self .web_url , str ):
215
+ if not isinstance (self .web_url , str ):
230
216
self .web_url = self .web_url .public_url
231
217
self .web_url += "/static/"
232
218
@@ -267,8 +253,8 @@ def handle_zmq(self, frames):
267
253
path = list (filter (lambda x : len (x ) > 0 , frames [1 ].decode ("utf-8" ).split ("/" )))
268
254
data = frames [2 ]
269
255
# Support caching of objects (note: even UUIDs have to match).
270
- cache_hit = (cmd == "set_object" and
271
- find_node (self .tree , path ).object and
256
+ cache_hit = (cmd == "set_object" and
257
+ find_node (self .tree , path ).object and
272
258
find_node (self .tree , path ).object == data )
273
259
if not cache_hit :
274
260
self .forward_to_websockets (frames )
@@ -379,8 +365,8 @@ def main():
379
365
parser .add_argument ('--open' , '-o' , action = "store_true" )
380
366
parser .add_argument ('--certfile' , type = str , default = None )
381
367
parser .add_argument ('--keyfile' , type = str , default = None )
382
- parser .add_argument ('--ngrok_http_tunnel' , action = "store_true" , help = """
383
- ngrok is a service for creating a public URL from your local machine, which
368
+ parser .add_argument ('--ngrok_http_tunnel' , action = "store_true" , help = """
369
+ ngrok is a service for creating a public URL from your local machine, which
384
370
is very useful if you would like to make your meshcat server public.""" )
385
371
results = parser .parse_args ()
386
372
bridge = ZMQWebSocketBridge (zmq_url = results .zmq_url ,
0 commit comments