Trisurf Monte Carlo simulator
Samo Penic
2016-07-06 c54425ba9bb4f9345e021e1a8daecdec8838b1dc
Missing files for remote connection
2 files added
129 ■■■■■ changed files
python/trisurf/Remote.py 73 ●●●●● patch | view | raw | blame | history
python/trisurf/WebTrisurf.py 56 ●●●●● patch | view | raw | blame | history
python/trisurf/Remote.py
New file
@@ -0,0 +1,73 @@
import paramiko
class Connection:
    def __init__(self, hostname, port=22, username=None, password=None):
        self.hostname=hostname
        self.port=port
        if(username!=None):
            self.username=username
        else:
            self.username=''
        if(password!=None):
            self.password=password
        else:
            self.password=''
        self.ssh=paramiko.SSHClient()
        self.ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        self.connected=False
        return
    def connect(self, Timeout=5):
        if(not self.connected):
            try:
                print("Trying to connect to: "+self.username+"@"+self.hostname+":"+str(self.port)+".")
                self.ssh.connect(self.hostname, username=self.username, password=self.password, port=self.port, timeout=Timeout)
                self.connected=True
            except:
                print("Error establishing connection with "+self.username+"@"+self.hostname+":"+str(self.port)+".")
                exit(1)
        else:
            print("Already connected!")
        return
    def disconnect(self):
        if(self.connected):
            try:
                self.ssh.close()
            except:
                print("Cannot disconect. Unknown error.")
        else:
            print("Cannot disconect. Already disconnected.")
        self.connected=False
    def execute(self,command):
        if(self.connected):
            try:
                stdin,stdout,stderr=self.ssh.exec_command(command)
                output=stdout.readlines()
                errors=stderr.readlines()
            #    print(errors)
                return(output)
            except:
                print("Cannot execute remote commands")
        else:
            print("Cannot execute remote commands. Connect first.")
    def send_file(self, local, remote):
        sftp=self.ssh.open_sftp()
        sftp.put(local,remote)
        sftp.close()
    def receive_file(self,remote,local):
        sftp=self.ssh.open_sftp()
        sftp.get(remote,local)
        sftp.close()
    def mkdir_remote(self,directory):
        sftp=self.ssh.open_sftp()
        sftp.mkdir(directory)
        sftp.close()
python/trisurf/WebTrisurf.py
New file
@@ -0,0 +1,56 @@
import subprocess
from urllib.parse import urlparse
import http.server
import socketserver
import sys, os
#Web server
class TsWEB(http.server.BaseHTTPRequestHandler):
    def do_GET(self):
        parsed_path=urlparse(self.path)
        """    message_parts = [
                'CLIENT VALUES:',
                'client_address=%s (%s)' % (self.client_address, self.address_string()),
                'command=%s' % self.command,
                'path=%s' % self.path,
                'real path=%s' % parsed_path.path,
                'query=%s' % parsed_path.query,
                'request_version=%s' % self.request_version,
                '',
                'SERVER VALUES:',
                'server_version=%s' % self.server_version,
                'sys_version=%s' % self.sys_version,
                'protocol_version=%s' % self.protocol_version,
                '',
                'HEADERS RECEIVED:',
                ]
        for name, value in sorted(self.headers.items()):
            message_parts.append('%s=%s' % (name, value.rstrip()))
        message_parts.append('')
        message = '<br>'.join(message_parts) """
        self.send_response(200)
        self.end_headers()
        self.wfile.write(b"<h1>Trisurf-ng manager web interface</h1><hr>")
        oldstdout=sys.stdout
        process=subprocess.Popen (['/usr/bin/python3', sys.argv[0], '-s', '--html'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        stdout, stderr= process.communicate()
        output=stdout.decode('ascii')
        output=output.replace('\n','<BR>')
        output=bytearray(output,'ascii')
        self.wfile.write(output)
class WebServer():
    def __init__(self, port=8000):
        http_server = socketserver.TCPServer(('', port), TsWEB)
        try:
            http_server.serve_forever()
        except KeyboardInterrupt:
            print('^C received, shutting down the web server')
            http_server.socket.close()