Stock flow log connections in Lambda - python

I'm actually doing a python script in AWS Lambda to transform statless logs from Flow Logs to statfull logs.
But I've some difficulties to save connections inside Lambda.
Variable "stock" is my list which will save connections
def init():
global stock
stock = []
def lambda_handler(event, context):
# retrieve bucket name and file_key from the S3 event
bucket_name = event['Records'][0]['s3']['bucket']['name']
file_key = event['Records'][0]['s3']['object']['key']
logger.info('Reading {} from {}'.format(file_key, bucket_name))
# get the object
obj = s3.get_object(Bucket=bucket_name, Key=file_key)
if obj['ContentType'] =='application/x-gzip' or key.endswith('.gz'):
lines_read = gzip.decompress(obj['Body'].read())
else:
lines_read = obj['Body'].read()
lines_read = lines_read[3:-3]
lines = lines_read.decode().split("\n")
for line in lines:
lines_split = line.split(" ")
if lines_split[17] == "3" or lines_split[17] == "19":
ts = str(time.time())
line_whrite = ts + " " + "Short Connection " + lines_split[5] + " " + lines_split[6] + " " + lines_split[7] + " " + lines_split[8] + " " + lines_split[16]
elif lines_split[17] == "1":
i = 0
ts = str(time.time())
json2 = {"time": lines_split[15], "srcaddr": lines_split[5], "dstaddr": lines_split[6], "srcport": lines_split[7], "dstport": lines_split[8]}
for connection in stock:
if json2["srcaddr"] == connection["srcaddr"] or connection["dstaddr"] and json2["dstaddr"] == connection["srcaddr"] or connection["dstaddr"] and json2["srcport"] == connection["srcport"] or connection["dstport"] and json2["dstport"] == connection["srcport"] or connection["dstport"]:
tpsstart = int(json2["time"])
tpsend = int(connection["time"])
converted_tpsstart = datetime.datetime.fromtimestamp(round(tpsstart / 1000))
converted_tpsend = datetime.datetime.fromtimestamp(round(tpsend / 1000))
tpsdiff = converted_tpsend - converted_tpsstart
line_whrite = "Connection Start :" + str(converted_tpsstart) + " Connection End:" + str(converted_tpsend) + " Connection Duration:" + str(tpsdiff) + " " + json2["srcaddr"] + " " + json2["dstaddr"] + " " + json2["srcport"] + " " + json2["dstport"]
del stock[i]
print(line_whrite)
i = i + 1
The problem is (I think) that my variable "stock" is always reset so empty...
Do you have some idea ?
Thanks :)

Related

using a variable from a json.load in another function

I am using an API for receiving TTN data from a device. i have created a function "def on_message(mqttc, obj, msg):", using a json.loads(msg.payload.decode('utf-8')) to receive mqtt data.
I want to pick the variable "node_data" and use in def devdata(). but it seems that i do not get anything but None.
import paho.mqtt.client as mqtt
import json
import pybase64
import binascii
APPEUI = "0018B24441524632"
APPID = "adeunis_fieldtester"
PSW = "ttn-account-v2.vuQczD1bmPoghhaKjlIHR-iHovHIbYMpfWSKosPAGaU"
# Call back functions
# gives connection message
def on_connect(mqttc, mosq, obj, rc):
print("Connected with result code:" + str(rc))
# subscribe for all devices of user
mqttc.subscribe('+/devices/+/up')
# gives message from device
def on_message(mqttc, obj, msg):
try:
x = json.loads(msg.payload.decode('utf-8'))
# metadata
app = x["app_id"]
device = x["dev_id"]
deveui = x["hardware_serial"]
port = x["port"]
confirmed = x["confirmed"]
counter = x["counter"]
payload_fields = x["payload_raw"]
datetime = x["metadata"]["time"]
gateways = x["metadata"]["gateways"]
frequency = x["metadata"]["frequency"]
modulation = x["metadata"]["modulation"]
data_rate = x["metadata"]["data_rate"]
air_time = x["metadata"]["airtime"]
coding_rate = x["metadata"]["coding_rate"]
for gw in gateways:
gateway_id = gw["gtw_id"]
timestamp = gw["timestamp"]
time = gw["time"]
channel = gw["channel"]
gwrssi = gw["rssi"]
gwsnr = gw["snr"]
# decoding the payload_field
payload_r = (pybase64.b64decode(payload_fields + "="))
# decoding the Payload_r to Byte-Ascii string
payload_h = binascii.hexlify(payload_r)
# Byte to tekst
node_data = (payload_h.decode("utf-8)")) #this is the variable i would like to use in devdata()
# Printing data, + str(payload_fields) + ", "
print(str(app) + ", " + str(device) + ", " + str(deveui) + ", " + str(port) + ", " + str(counter) + ", "
+ str(node_data) + ", " + str(modulation) + ", " + str(datetime) + ", " + str(frequency) + ", "
+ str(confirmed) + ", " + str(data_rate) + ", " + str(air_time) + ", " + str(coding_rate) + ", "
+ str(gateway_id) + ", " + str(timestamp) + ", " + str(time) + "," + str(channel) + ", "
+ str(gwrssi) + ", " + str(gwsnr))
return node_data #return data for use in devdata()
except Exception as e:
print(e)
pass
def devdata():
node_data = on_message() # trying to use this as the Variable
temperatur = int(node_data[2:4], 16)
print("temperatur =", temperatur)
`

A multi-input PySimpleGUI for a working python script

I'm using a working script built in python 3 that reads an excel file. Using the input, it figures out which VIP (Virtual IP) subnet I should use and which F5 load balancer the VIP should be matched with, and then spits out the cli commands needed to configure on the load balancer as another file. I want to know if it's possible to add a basic GUI to this using the code I have below. Based on that, it will create a file on my desktop with the commands I need to configure taken from the excel file and input given. If the information is blank within excel it will prompt and ask you. An edited example of a simple working script that runs is shown below:
import pandas as pd
import datetime
from netaddr import IPNetwork, IPAddress, IPSet
import os
import glob
import shutil
def createfolder(directory):
try:
if not os.path.exists(directory):
os.makedirs(directory)
except OSError:
print('Error: Creating directory. ' + directory)
global options
global member
global lb_mode
global pool_member1
global pool_member2
global pool_member3
global pool_member4
global cert
global iRule1
global iRule2
global rules
global one_connect
global http_profile
global primary_persist
global fallback_persist
global snat
global rule_member_1
global rule_member_2
global rule_member_3
global irules
global profiles
global loadbalancer
global subnet
global ssl_profile_name
global ssl_config
x = datetime.datetime.now()
change_order = input("What is the change order for this VIP? ")
lbarf_file = str(input("Enter the full path of the LBARF form. ").strip('\"'))
description = "description " + change_order
df = pd.read_excel(lbarf_file)
# Info pulled from the LBARF
service_port_input = str(df.iloc[13, 2])
http_profile_input = df.iloc[33, 2]
primary_persist_input = df.iloc[34, 2]
fallback_persist_input = df.iloc[35, 2]
iRule1_input = df.iloc[42, 2]
iRule2_input = df.iloc[43, 2]
snat_input = df.iloc[47, 2]
one_connect_input = df.iloc[48, 2]
fqdn = str(df.iloc[12, 2]).lower()
member1 = str(df.iloc[19, 2])
member2 = str(df.iloc[20, 2])
member3 = str(df.iloc[21, 2])
member4 = str(df.iloc[22, 2])
pool_port1 = str(df.iloc[19, 4])
pool_port2 = str(df.iloc[20, 4])
pool_port3 = str(df.iloc[21, 4])
pool_port4 = str(df.iloc[22, 4])
lb_mode_input = df.iloc[32, 2]
pool_name = str(fqdn + "-" + pool_port1)
monitor_input = df.iloc[36, 2]
# Pool builder
if member1 == "nan":
pool_member1 = ""
elif member1 != "nan":
pool_member1 = member1 + ":" + pool_port1 + " "
else:
pass
if member2 == "nan":
pool_member2 = ""
elif member2 != "nan":
pool_member2 = member2 + ":" + pool_port2 + " "
else:
pass
if member3 == "nan":
pool_member3 = ""
elif member3 != "nan":
pool_member3 = member3 + ":" + pool_port3 + " "
else:
pass
if member4 == "nan":
pool_member4 = ""
elif member4 != "nan":
pool_member4 = member4 + ":" + pool_port4 + " "
else:
pass
if lb_mode_input == "Round Robin":
lb_mode = "load-balancing-mode round-robin "
elif lb_mode_input == "Least connections":
lb_mode = "load-balancing-mode least-connections-node "
elif lb_mode_input == "Observed":
lb_mode = "load-balancing-mode observed-node "
else:
pass
if monitor_input == "TCP":
monitor = "monitor tcp "
elif monitor_input == "TCP 30 seconds":
monitor = "monitor tcp-30 "
elif monitor_input == "Self-Service":
monitor = "monitor self-service "
elif monitor_input == "None":
monitor = ""
else:
monitor = " monitor " + input("Please enter the health monitor name: ") + " "
member = IPAddress(member1)
# Lists for each LB pair containing the node subnets.
loadbalancer1_node_IPs = IPSet([IPNetwork('1.1.1.0/22'), IPNetwork('2.2.2.0/22'), IPNetwork('3.3.3.0/22')])
loadbalancer2_node_IPs = IPSet([IPNetwork('4.4.4.0/23'), IPNetwork('2.2.2.0/23'), IPNetwork('3.3.3.0/23')])
# Provides the LB the VIP is to be built on based on node IP address.
if IPAddress(member) in loadbalancer1_node_IPs:
loadbalancer = "The LB pair you need to build this VIP on is loadbalancer1_node_IPs"
subnet = "You need a VIP ip from subnet 1.1.1.0/22, 2.2.2.0/22, or 3.3.3.0/22"
elif IPAddress(member) in loadbalancer2_node_IPs:
loadbalancer = "The LB pair you need to build this VIP on is loadbalancer2_node_IPs"
subnet = "You need a VIP ip from subnet 4.4.4.0/23, 2.2.2.0/23, or 3.3.3.0/23"
else:
pass
print(loadbalancer)
print(fqdn)
print(subnet)
vip_ip_input = IPAddress(input("Which VIP IP did you choose? "))
vip_ip = str(vip_ip_input) + str(":") + str(service_port_input)
ip = str(vip_ip_input)
if iRule1_input == "_sys_https_redirect":
iRule1 = "_sys_https_redirect "
elif iRule1_input == "x-Forwarding":
iRule1 = "x-forwarding "
else:
iRule1 = ""
if iRule2_input == "_sys_https_redirect":
iRule2 = "_sys_https_redirect "
elif iRule2_input == "x-Forwarding":
iRule2 = "x-forwarding "
else:
iRule2 = ""
if snat_input == "Auto Map":
snat = "snat automap "
else:
snat = ""
if one_connect_input == "oneconnect":
one_connect = "oneconnect "
else:
one_connect = ""
if http_profile_input == "http":
http_profile = "http "
else:
http_profile = ""
if primary_persist_input == "Source Addr":
primary_persist = " persist replace-all-with { source_addr } "
elif primary_persist_input == "Dest Addr":
primary_persist = " persist replace-all-with { dest_addr } "
elif primary_persist_input == "Cookie":
primary_persist = " persist replace-all-with { cookie } "
else:
primary_persist = ""
if fallback_persist_input == "Source Addr":
fallback_persist = " fallback-persistence source_addr "
elif fallback_persist_input == "Dest Addr":
fallback_persist = " fallback-persistence dest_addr "
elif fallback_persist_input == "Cookie":
fallback_persist = " fallback-persistence cookie "
else:
fallback_persist = ""
folder = r'C:\Users\username\Desktop\LBARF script' + str(change_order)
createfolder(folder)
fh = open(folder + "/" + str(change_order) + ".txt", "w")
fh.write("This program is a test. I assume no responsibility." + "\r" +
"Please verify everything looks correct." + "\r" + "\r" + "This script was run on " +
x.strftime("%x") + " # " + x.strftime("%I") + ":" + x.strftime("%M") + " " + x.strftime("%p") +
" for change order " + change_order + "\r" + "\r" + fqdn + "\r" + ip + "\r" + loadbalancer + "\r" + "\r")
fh.write("create ltm pool " + pool_name + " members add { " + pool_member1 + pool_member2 + pool_member3 +
pool_member4 + " } " + monitor + lb_mode + description + "\r" + "\r")
if service_port_input == "80 and 443":
service_port_input = "80"
else:
pass
if service_port_input == "80":
if iRule1_input == "_sys_https_redirect":
port_80_redirect_vip = ("create ltm virtual " + fqdn + "-80 destination " + ip +
":80 profiles add { http } snat automap rules { _sys_https_redirect } "
+ description + "\r" + "\r")
fh.write(port_80_redirect_vip)
print("Port 80 VIP is complete, We will now move on to the 443 VIP")
service_port_input = "443"
irules = "rules { " + iRule2 + " } "
elif iRule2_input == "_sys_https_redirect":
port_80_redirect_vip = ("create ltm virtual " + fqdn + "-80 destination " + ip +
":80 profiles add { http } snat automap rules { _sys_https_redirect } "
+ description + "\r" + "\r")
fh.write(port_80_redirect_vip)
print("Port 80 VIP is complete, We will now move on to the 443 VIP")
service_port_input = "443"
irules = "rules { " + iRule2 + " } "
else:
vip_build = ("create ltm virtual " + fqdn + "-" + service_port_input + " destination " + ip + ":" +
service_port_input + " pool " + pool_name + " profiles add { " + http_profile + " } " +
primary_persist + fallback_persist + description + "\r" + "\r")
fh.write(vip_build)
service_port_input = "443"
else:
pass
if iRule1.__contains__("sys"):
rules = "rules { " + iRule2 + " } "
elif iRule2.__contains__("sys"):
rules = "rules { " + iRule1 + " } "
else:
rules = "rules { " + iRule1 + iRule2 + " } "
if service_port_input == "443":
ssl_profile_input = input("Will you be applying an SSL profile? y or n: ").lower()
if ssl_profile_input.__contains__("y"):
cert = input("Installing a cert? y or n: ").lower()
if cert.__contains__("y"):
ssl_profile_name = fqdn + "-clientssl"
upload_name = fqdn + "-" + x.strftime("%Y")
print("**** SSL PROFILE CONFIG *****")
print("**** Upload Cert/Key to " + loadbalancer + " with the name of " + upload_name)
ssl_cert = fqdn + "-" + x.strftime("%Y") + ".crt "
ssl_key = fqdn + "-" + x.strftime("%Y") + ".key "
chain_input = input("Will a chain be applied? y or n: ").lower()
if chain_input.__contains__("y"):
chain = " chain " + input("What is the name of the chain? ")
else:
chain = ""
ciphers_input = input("Will ciphers be applied? ").lower()
if ciphers_input.__contains__("y"):
ciphers = "ciphers " + input("Enter Ciphers here: ")
else:
ciphers = ""
ssl_config = ("create ltm profile client-ssl " + ssl_profile_name + " cert " + ssl_cert + " key " +
ssl_key + ciphers + " " + chain + description + "\r" + "\r")
else:
ssl_profile_name = input("What is the SSL profile name? ")
else:
ssl_profile_name = ""
cert = ""
key = ""
chain = ""
ciphers = ""
ssl_config = ""
else:
ssl_profile_name = ""
cert = ""
key = ""
chain = ""
ciphers = ""
ssl_config = ""
profile = "profiles add { " + one_connect + http_profile + ssl_profile_name + " } "
if profile == "profiles add { } ":
profiles = ""
else:
profiles = ("profiles add { " + one_connect + http_profile + ssl_profile_name + " } ")
options = (profiles + snat + primary_persist + fallback_persist + rules + description)
if cert.__contains__("y"):
fh.write(ssl_config)
else:
pass
fh.write("create ltm virtual " + fqdn + "-" + service_port_input + " destination " + ip + ":" + service_port_input +
" pool " + pool_name + " " + options + "\r" + "\r")
fh.close()
key_files = 'C:/Users/username/Downloads/*.key'
crt_files = 'C:/Users/username/Downloads/*.crt'
cer_files = 'C:/Users/username/Downloads/*.cer'
pem_files = 'C:/Users/username/Downloads/*.pem'
pfx_files = 'C:/Users/username/Downloads/*.pfx'
filelist = glob.glob(key_files)
for single_file in filelist:
shutil.move(single_file, folder)
filelist = glob.glob(crt_files)
for single_file in filelist:
shutil.move(single_file, folder)
filelist = glob.glob(cer_files)
for single_file in filelist:
shutil.move(single_file, folder)
filelist = glob.glob(pem_files)
for single_file in filelist:
shutil.move(single_file, folder)
filelist = glob.glob(pfx_files)
for single_file in filelist:
shutil.move(single_file, folder)
# os.rename(lbarf_file, 'C:/Users/username/Downloads/' + change_order + ".xlsx")
shutil.move(lbarf_file, folder)
print("The CLI commands for this VIP are located at " + folder)
exit(input("Press Enter to exit."))
It was suggested to me to put the script as a function and import it to the code for the GUI which I did and named it vipTest. Next I imported it to PySimpleGUI I am using to create a simple form with here:
import vipTest
import PySimpleGUI as sg
# Very basic form. Return values as a list
form = sg.FlexForm('VIP Builder') # begin with a blank form
layout = [
[sg.Text('Please enter the information below')],
[sg.Text('What is the change order for this VIP?: ', size=(30, 1)), sg.InputText('change #')],
[sg.Text('Enter the full path of the LBARF form: ', size=(30, 1)), sg.InputText('location path')],
[sg.Text('Which VIP IP did you choose?: ', size=(30, 1)), sg.InputText('ip address of vip')],
[sg.Submit(), sg.Cancel()]
]
lbarf_file, folder = vipTest.createfolder(directory)
button, values = form.Layout(layout).Read()
I am having trouble getting an output from the GUI of the results. When I run this program in PyCharm the GUI does not open and script runs in the console. Even if I can get the GUI to work with a specific example, I am concerned I might need to have the GUI add a text field each time the information in the excel file is blank. I would like to ultimately like to display the config commands from the gui but am not getting anywhere with it as is. Any assistance on this is greatly appreciated. Thank you.

python multithreading in loops

I think I have a very basic understanding of how threading works, but since I don't know that much I can't figure this out. I want to have a pool limit of about 10 threads, but the tricky part is I don't know how to make it read line by line.
proxies = {
'http': 'http://123.10.210.213:9999',
'https': 'http://123.10.210.213:9999'
}
def create_proxy_lst(txt):
print("""
########################################
# WORKING | NOT WORKING #
########################################
""")
proxy_list = []
with open(txt) as f:
for line in f:
proxy_list.append(line.strip('\n'))
return proxy_list
def check_proxy(website="https://google.com/"):
working = 0
not_working = 0
total = 0
lst = create_proxy_lst("uncheckedproxys.txt")
for proxy in lst:
try:
proxies["https"] = "http://" + proxy
proxies["http"] = "http://" + proxy
r = requests.get(website, timeout=1, proxies=proxies)
if r.status_code == 200:
print("%s" % proxy)
working += 1
total += 1
os.system("title Working: " + str(working) + "\t Not working " + str(not_working) + " ✔" + " Total: " + str(total) + "/" + str(len(lst)))
except Exception:
print("\t\t %s" % proxy)
not_working += 1
total += 1
os.system("title Working: " + str(working) + "\t Not working " + str(not_working) + " ✖" + " Total: " + str(total) + "/" + str(len(lst)))
Put your proxies into a Queue.Queue(), and then start 10 threads to read proxy from the queue.
In your case:
from Queue import Queue
from threading import Thread
def worker(proxy_queue):
while not proxy_queue.empty():
proxy = proxy_queue.get()
working = 0
not_working = 0
total = 0
try:
proxies["https"] = "http://" + proxy
proxies["http"] = "http://" + proxy
r = requests.get(website, timeout=1, proxies=proxies)
if r.status_code == 200:
print("%s" % proxy)
working += 1
total += 1
os.system("title Working: " + str(working) + "\t Not working " + str(not_working) + " ✔" + " Total: " + str(total) + "/" + str(len(lst)))
except Exception:
print("\t\t %s" % proxy)
not_working += 1
total += 1
os.system("title Working: " + str(working) + "\t Not working " + str(not_working) + " ✖" + " Total: " + str(total) + "/" + str(len(lst)))
if __name__ == '__main__':
# Build a queue
proxy_queue = Queue()
# Put these proxies into the queue
with open("uncheckedproxys.txt") as f:
for line in f:
proxy_queue.put(line.strip())
# Create thread pool
thread_pool = [Thread(target=worker, args=proxy_queue) for i in range(10)]
# Start threads
for thread in thread_pool:
thread.start()

why there is a "SyntaxError: invalid syntax"

Error message:
runfile('//rschfs1x/userrs/xd73_RS/Documents/Python Scripts/test_515.py', wdir='//rschfs1x/userrs/xd73_RS/Documents/Python Scripts')
File "//rschfs1x/userrs/xd73_RS/Documents/Python Scripts/test_515.py", line 120
if __name__ == "__main__":
^
SyntaxError: invalid syntax
Really not sure why thre is this problem
def sendemail(alertmessage):
msg = MIMEText(alertmessage)
msg['Subject'] = 'Patent Data Error Message'
msg['From'] = "patentcornell#gmail.com"
msg['To'] = "patentcornell#gmail.com"
# Credentials (if needed)
username = 'patentcornell#gmail.com'
password = ''
# The actual mail send
server = smtplib.SMTP('smtp.gmail.com:587')
server.starttls()
server.login(username,password)
server.sendmail("hello", "xd73#cornell.edu", alertmessage)
server.sendmail("hello", "jq58#cornell.edu", alertmessage)
#server.sendmail("hello", "sl2448#cornell.edu", alertmessage)
server.sendmail("hello", "patentcornell#gmail.com", alertmessage)
'''
Shanjun Li <sl2448#cornell.edu>,
Panle Barwick <pjb298#cornell.edu>,
Jing Qian <jq58#cornell.edu>
'''
server.quit()
def main(year = 1985, starting_page = 2, filepath = ""):
time1 = time.time()
print "start timing: " + str(time1)
driver = webdriver.Firefox()
driver.get("http://epub.sipo.gov.cn/")
elem = driver.find_element_by_id("soso_text")
f = open( filepath + "year_test_" + str(year), "wb")
flog = open(filepath + "log_" + str(year), "wb")
driver.get("http://epub.sipo.gov.cn/")
elem = driver.find_element_by_id("soso_text")
elem.send_keys(str(year))
elem.send_keys(Keys.RETURN)
content = driver.page_source.encode('utf-8')
for uchar in content:
f.write(bytearray([ord(uchar)]))
flog.write(str(year) + " " + str(1) + "\n")
case = 1
nextpage = driver.find_element_by_xpath("/html/body/div[3]/div[2]/div[4]/a[7]")
turnto = driver.find_element_by_xpath("/html/body/div[3]/div[2]/div[4]/span")
print "hello 0"
print nextpage.get_attribute("innerHTML")
totalnum = int(nextpage.get_attribute("innerHTML"))
print "totalnum: " + str(totalnum)
#try:
# from which page we start downloading, the starting page
for i in range(starting_page, totalnum + 1):
timeinterval_1 = time.time()
print str(year) + " page: " + str(i)
#turnto = driver.find_element_by_xpath("/html/body/div[3]/div[2]/div[4]/span")
turnto = driver.find_element_by_id('pn')
turnto.send_keys(str(i))
turnto.send_keys(Keys.ENTER)
#turnto.submit()
content = driver.page_source.encode('utf-8')
# test file writing speed
time_file_start = time.time()
for uchar in content:
f.write(bytearray([ord(uchar)]))
f.write("\n")
#robust Check
print "interval: " + str(timeinterval_2 - timeinterval_1)
if timeinterval_2 - timeinterval_1 > 60:
flog.write("lost: " + str(year) + " page: " + str(i) + "\n")
print "too long to load " + str(year) + " " + str(i)
continue
else:
flog.write(str(year) + " " + str(i) + "\n")
continue
#except ValueError as err:
print(err.args)
sendmail("xd73_RS: " + err.args + " " + str(time.time())
if __name__ == "__main__":
filepath = "U:/Documents/Python Scripts/test_data_515/"
#sendemail("test email function!")
main(2010, 2, filepath)
This line:
sendmail("xd73_RS: " + err.args + " " + str(time.time())
Has unmatched parentheses. Generally if the syntax error occurs on line n, its a good idea to check the syntax on line n-1.

Mysql data to be save as CSV/Excel using flask-python when button click

I'm new at exporting data, I research all over the net but it was really hard for me to understand, can someone help me to know the basic about it.
This is my main problem: I want to download a specific data from mysql base on the date range I choose in my client, then when I click the download button, I want these data from mysql to be save in my computer together the user have the option to save it as CSV/Excel, I'm using python for my webservice. Thank you
This is my code right know in my webservice:
#api.route('/export_file/', methods=['GET', 'POST'])
def export_file():
if request.method == 'POST':
selectAttendance = """SELECT * FROM attendance"""
db.session.execute(selectAttendance)
db.session.commit()
f = csv.writer(open("file.csv", "w"))
for row in selectAttendance:
f.writerow([str(row)])
return jsonify({'success': True})
In general:
Set the mime header "Content-Type" part of the http header to the corresponding MIME-Type matching your data.
This tells the browser what type of data the webserver is going to send.
Send the actual data in the 'body'
With flask:
Forcing application/json MIME type in a view (Flask)
http://flask.pocoo.org/docs/0.10/patterns/streaming/
def get(self):
try:
os.stat(BACKUP_PATH)
except:
os.mkdir(BACKUP_PATH)
now = datetime.now() # current date and time
year = now.strftime("%Y")
month = now.strftime("%m")
day = now.strftime("%d")
time = now.strftime("%H:%M:%S")
date_time = now.strftime("%d_%m_%Y_%H:%M:%S")
TODAYBACKUPPATH = BACKUP_PATH + '/' + date_time
try:
os.stat(TODAYBACKUPPATH)
except:
os.mkdir(TODAYBACKUPPATH)
print ("checking for databases names file.")
if os.path.exists(DB_NAME):
file1 = open(DB_NAME)
multi = 1
print ("Databases file found...")
print ("Starting backup of all dbs listed in file " + DB_NAME)
else:
print ("Databases file not found...")
print ("Starting backup of database " + DB_NAME)
multi = 0
if multi:
in_file = open(DB_NAME,"r")
flength = len(in_file.readlines())
in_file.close()
p = 1
dbfile = open(DB_NAME,"r")
while p <= flength:
db = dbfile.readline() # reading database name from file
db = db[:-1] # deletes extra line
dumpcmd = "mysqldump -h " + DB_HOST + " -u " + DB_USER + " -p" + DB_USER_PASSWORD + " " + db + " > " + pipes.quote(TODAYBACKUPPATH) + "/" + db + ".sql"
os.system(dumpcmd)
gzipcmd = "gzip " + pipes.quote(TODAYBACKUPPATH) + "/" + db + ".sql"
os.system(gzipcmd)
p = p + 1
dbfile.close()
else:
db = DB_NAME
dumpcmd = "mysqldump -h " + DB_HOST + " -u " + DB_USER + " -p" + DB_USER_PASSWORD + " " + db + " > " + pipes.quote(TODAYBACKUPPATH) + "/" + db + ".sql"
os.system(dumpcmd)
gzipcmd = "gzip " + pipes.quote(TODAYBACKUPPATH) + "/" + db + ".sql"
os.system(gzipcmd)
# t = ("Your backups have been created in '" + TODAYBACKUPPATH + "' directory")
return "Your Folder have been created in '" + TODAYBACKUPPATH + "'."

Categories