Skip to content

Commit

Permalink
Merge pull request #155 from ghutchis/update-remote-script
Browse files Browse the repository at this point in the history
Update script for py3 and cross-platform TMPDIR access
  • Loading branch information
ghutchis authored May 15, 2021
2 parents 143f146 + b2a4967 commit ae19117
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions scripts/avogadro-remote.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
#!/usr/bin/python

from __future__ import print_function

import sys
import json
import time
import socket
import struct
import tempfile

class Connection:
def __init__(self, name = "avogadro"):
Expand All @@ -13,15 +16,15 @@ def __init__(self, name = "avogadro"):
socket.SOCK_STREAM)

# connect
self.sock.connect("/tmp/" + name)
self.sock.connect(tempfile.gettempdir() + '/' + name)

def send_json(self, obj):
self.send_message(json.dumps(obj))

def send_message(self, msg):
sz = len(msg)
hdr = struct.pack('>I', sz)
pkt = hdr + msg
pkt = hdr + msg.encode('ascii')
self.sock.send(pkt)

def recv_message(self, size = 1024):
Expand All @@ -35,7 +38,7 @@ def recv_json(self):
try:
return json.loads(msg)
except Exception as e:
print 'error: ' + str(e)
print('error: ' + str(e))
return {}

def close(self):
Expand Down Expand Up @@ -69,9 +72,9 @@ def close(self):
)

else:
print 'unknown method: ' + method
sys.exit(-1)
print('unknown method: ' + method)
conn.close()
sys.exit(-1)

print 'reply: ' + str(conn.recv_message())
print('reply: ' + str(conn.recv_message()))
conn.close()

0 comments on commit ae19117

Please sign in to comment.