pyclo_functions.py 2.3 KB
Newer Older
Arne Striegler's avatar
v1.0.0  
Arne Striegler committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
#!/usr/bin/env pytsftp://104.199.77.123/var/www/html/ul/upload.php
# -*- coding: utf-8 -*-
"""
Created on Thu Nov  2 21:26:04 2017

@author: arne
"""

import MySQLdb
import numpy as np
import pycurl
from StringIO import StringIO

########################################################################
def mysql_get(sql):
    "Read data from DB"
    db = MySQLdb.connect(host="130.211.56.253", user="root", passwd="pyphodb", db="pypho") 

    cur = db.cursor()
    cur.execute( sql )

    
    db.close()
    return cur.fetchall()


########################################################################
def mysql_set(sql):
    "Write data to DB"
    db = MySQLdb.connect(host="130.211.56.253", user="root", passwd="pyphodb", db="pypho") 

    cur = db.cursor()
    number_of_rows = cur.execute(sql)
    db.commit( )

    
    db.close()
    return number_of_rows


########################################################################
def cloud_LoadFiles():
    "Save, zip and send all files for cloude computing"
    import json
    Etmp = np.loadtxt('E.txt').view(complex)
    
    with open('glova.json') as json_data:
        glova = json.load(json_data)
        
    with open('link.json') as json_data:
        link = json.load(json_data)
        
    return Etmp, glova, link


########################################################################
def local_SaveFiles(filename, E):
    np.savetxt(filename + '.txt', E[0]['E'].view(float) )

    try:
        import zlib
        import zipfile
        compression = zipfile.ZIP_DEFLATED
    except:
        compression = zipfile.ZIP_STORED

    zf = zipfile.ZipFile(filename +".zip", mode='w')
    try:
        zf.write(filename + '.txt', compress_type=compression)        
    finally:        
        zf.close()
    
    
########################################################################
def cloud_SaveFiles(cid, filename):    
 # Send files to cloud    
    print ('Sending files to master!')
    c = pycurl.Curl()
    buffer1 = StringIO()
    c.setopt(c.URL, 'https://optical-fiber-transmission-simulation.com/ul/upload.php')
    c.setopt(c.POST, 1)
    c.setopt(c.HTTPPOST, [("sim_ticket", cid), ("filecontents", (c.FORM_FILE, filename))])

    c.setopt(c.WRITEDATA, buffer1)
    c.perform()
    c.close()
    body1 = buffer1.getvalue()
    
    return body1[0:74]