I am doing wsgi python and calling aws workspaces api to get all the workspaces and render on the screen (webpage). But I see my logs, that the function is getting called twice just by hitting the index URL.
server side
ws_butt='get'
request_body_size = int(environ.get('CONTENT_LENGTH', 0))
request_body = environ['wsgi.input'].read(request_body_size)
d = parse_qs(request_body)
ws_butt=d.get('ws_butt',['get'])[0]
if ws_butt.lower()=='get':
#calls describe_workspaces to fetch all the workspaces for the user
new_content=[]
for region,endpoint in region_endpoint_dict.iteritems():
code,content=describe_workspaces(region,endpoint[0],session_username=username)
#some processing on content and generate new_content
if new_content:
#some internal processing of replacing strings
return your_workspace_window(environ, start_response,my_response=new_content)
client side
start_response('200 OK', [('Content-Type', 'text/html')])
html_output='''
<html>
<head>
<meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate" />
<meta http-equiv="Pragma" content="no-cache" />
<meta http-equiv="Expires" content="0" />
<meta name="viewport" content="width=device-width,initial-scale=1">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
</head>
<body>
<form id="form_myws" method="POST">
<input type="hidden" name="workspaceid" value="%s">
<input type="hidden" name="type" value="%s">
<input type="hidden" name="region" value="%s">
<input type="hidden" name="state" value="%s">
<input type="hidden" name="ipaddress" value="%s">
<p><center><input type="submit" value="Reboot" name="ws_butt" id="submit" class="submit"/></center></p>
</body>
'''
#some more processing on client side
What is causing it to call "describe_workspaces" in forloop to be called >=2 times for every region? Check network elements as well and all seems fine with 200OK so I dont know what is causing so called reload.
Related
I built the interface of a python script that sends sms using an api, it works normally but quad I try to send a message from the interface nothing happens, here is my code
This is main.py file
import eel
import os
from twilio.rest import Client
dirname = os.path.dirname('/Users/Admin/Desktop/App/')
eel.init(os.path.join(dirname,'web/'))
#eel.expose
def send_message(number, receiver, message):
# send sms
account_sid = ''
auth_token = ''
client = Client(account_sid, auth_token)
message = client.messages.create(from_=number, body=message, to=receiver)
print(message.sid)
eel.start('index.html')
script.js
document.getElementById("btn").addEventListener("click", function() {
var name = document.getElementById("number").value;
var email = document.getElementById("receiver").value;
var message = document.getElementById("message").value;
send_message(number, receiver, message);
});
function send_message(number, receiver, message) {
eel.send_message(number, receiver, message)(function(result) {
console.log(result);
});
}
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>App</title>
</head>
<body>
<div class="form">
<form onsubmit="eel.send_message(this.number.value, this.receiver.value, this.message.value); return false;">
<input type="number" name="name" id="number" placeholder="your number" required>
<input type="number" name="number" id="receiver" placeholder="Receiver" required>
<textarea name="message" id="message" cols="30" rows="10" placeholder="Enter message" required></textarea>
<input type="submit" value="Send" id="btn">
</form>
</div>
<script type="text/javascript" src="script.js"></script>
</body>
</html>
I am trying to build an application that allows me to send sms from an interface using the eel framework
I'm trying to create a webserver with micropython in access point (hotspot) mode. My problem is that I don't know how to pass simple HTML user input to a python variable (username and password).
Flask/MicroWebSrv is not an option for me due to memory issues (using an ESP8266 with limited flash and RAM) and I don't really want to rebuild my webserver from scratch. Previous solutions based on CGI package is depreciated. Any other generic method?
import network
import socket
ap = network.WLAN(network.AP_IF)
ap.active(True)
ap.config(essid=ap_ssid, password=ap_psw)
s = socket.socket()
s.bind(('', 80))
s.listen(5)
def webpage():
html = """
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1", charset="UTF-8">
<title>Login Form</title>
</head>
<body>
<div class="center">
<div class="header">
Login Form
</div>
<form method="post">
<input type="text" placeholder="Username" id="wifi_ssid" name="wifi_ssid">
<i class="far fa-envelope"></i>
<input id="wifi_psw" name="wifi_psw" type="password" placeholder="Password">
<i class="fas fa-lock" onclick="show()"></i>
<input type="submit" value="Sign in">
</form>
</div>
</body>
</html>"""
return html
while True:
conn, addr = s.accept()
request = conn.recv(1024)
response = webpage()
conn.send(response)
conn.close()
I want to download 10 years of modis monthly data, for which I have links as lines in a text file. I used the following code:
import requests
import os
import urllib
file = open("link.txt", "r")
Lines = file.readlines()
for line in Lines:
filename= line.strip()
print(filename)
user, password= 'xx#gmail.com', 'yy#123'
r = requests.get(filename, auth=(user, password), allow_redirects=True)
open(str(filename[48:90]), 'wb').write(r.content)
The files getting downloaded are 11kb each and corrupted. I am unable to figure out what is the problem here. The content of the output file is mentioned below.
link.txt contains files like these:
https://oceandata.sci.gsfc.nasa.gov/cgi/getfile/AQUA_MODIS.20101201_20101231.L3b.MO.CHL.nc
https://oceandata.sci.gsfc.nasa.gov/cgi/getfile/AQUA_MODIS.20110101_20110131.L3b.MO.CHL.nc
https://oceandata.sci.gsfc.nasa.gov/cgi/getfile/AQUA_MODIS.20110201_20110228.L3b.MO.CHL.nc
https://oceandata.sci.gsfc.nasa.gov/cgi/getfile/AQUA_MODIS.20110301_20110331.L3b.MO.CHL.nc
Output nc file looks like this:
<!DOCTYPE html>
<!--[if lt IE 7]><html class="no-js lt-ie9 lt-ie8 lt-ie7"> <![endif]-->
<!--[if IE 7]><html class="no-js lt-ie9 lt-ie8"> <![endif]-->
<!--[if IE 8]><html class="no-js lt-ie9"> <![endif]-->
<!--[if gt IE 8]><!--><html lang="en" class="no-js"><!--<![endif]-->
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title>Earthdata Login</title>
<meta name="description" content="Earthdata Login">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!-- Google Tag Manager -->
<script>(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push(
{'gtm.start': new Date().getTime(),event:'gtm.js'}
);var f=d.getElementsByTagName(s)[0],
j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
})(window,document,'script','dataLayer','GTM-WNP7MLF');</script>
<!-- End Google Tag Manager -->
<link href="https://cdn.earthdata.nasa.gov/eui/1.1.3/stylesheets/application.css" rel="stylesheet" />
<link rel="stylesheet" media="all" href="/assets/application-432b3917d4a41042c0fd963eba859548ef2993f5ed7a0dca4bdb446fdf807556.css" />
<!--[if IE 7]>
<link rel="stylesheet" href="/assets/font-awesome-ie7.min.css">
<![endif]-->
<link href="//netdna.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css" rel="stylesheet">
<link href='https://fonts.googleapis.com/css?family=Source+Sans+Pro:300,700' rel='stylesheet' type='text/css'>
<meta name="csrf-param" content="authenticity_token" />
<meta name="csrf-token" content="4cewX3n0xOGOTr5gWHgIlZctPtRm55YQ9lohmdPuvq1SfDO/rNhsQZZB3OUPRDLGsqQ/QG9uVqxV5yaLx9GqAQ==" />
<!-- Grid background: http://subtlepatterns.com/graphy/ -->
</head>
<body class="oauth authorize" data-turbolinks-eval=false>
<!-- Google Tag Manager (noscript) -->
<noscript>
<iframe src="https://www.googletagmanager.com/ns.html?id=GTM-WNP7MLF"
height="0" width="0" style="display:none;visibility:hidden"></iframe>
</noscript>
<!-- End Google Tag Manager (noscript) -->
<header id="earthdata-tophat2" style="height: 32px;"></header>
<!--[if lt IE 7]>
<p class="chromeframe">You are using an <strong>outdated</strong> browser. Please upgrade your browser or activate Google Chrome Frame to improve your experience.</p>
<![endif]-->
<div class="container">
<header role="banner">
<div id="masthead-logo">
<h1><a class="ir" href="/">Earthdata Login</a></h1>
<span class="eui-badge badge daac">Earthdata Login</span>
</div>
<a id="hamburger" href="#"><img title="Mobile Menu" alt="Three horizontal lines stacked" src="/assets/hamburger-68c8505066427f3e3f6ee40b24cfd3c9f7c0fe93ee298b9046564637262115fa.png" /></a>
<nav role="navigation" class="masthead">
<div id="hide">
<ul>
</ul>
</div>
</nav>
</header>
<section id="callout-login">
<div class="client-login">
<br>
<h3 class="client-description">
</h3>
</div>
<form id="login" action="/login" accept-charset="UTF-8" method="post"><input name="utf8" type="hidden" value="✓" autocomplete="off" /><input type="hidden" name="authenticity_token" value="amGKbzpZc+qTleRHBa9q8W4OvZNAUzhOfGu1G9gX0QnZ2gmP73XbSouahsJSk1CiS4e8B0na+PLf1rIJzCjFpQ==" autocomplete="off" />
<p><label for="username">Username</label><i class="fa fa-question-circle fa-question-circle--blue user-name" title="Login using either your Username or Email Address"></i><input type="text" name="username" id="username" autofocus="autofocus" class="default" /></p>
<p><label for="password">Password</label><br /><input type="password" name="password" id="password" /></p>
<p><input type="hidden" name="client_id" id="client_id" value="Z0u-MdLNypXBjiDREZ3roA" autocomplete="off" /></p>
<p><input type="hidden" name="redirect_uri" id="redirect_uri" value="https://oceandata.sci.gsfc.nasa.gov/ob/getfile/restrict" autocomplete="off" /></p> <p><input type="hidden" name="response_type" id="response_type" value="code" autocomplete="off" /></p>
<p><input type="hidden" name="state" id="state" autocomplete="off" /></p>
<p><input type="checkbox" name="stay_in" id="stay_in" value="1" checked="checked" /> <label for="stay_in">Stay signed in (this is a private workstation)</label></p>
<p class="button-with-notes">
<input type="submit" name="commit" value="Log in" class="eui-btn--round eui-btn--green" data-disable-with="Log in" />
<a class="eui-btn--round eui-btn--blue" href="/users/new?client_id=Z0u-MdLNypXBjiDREZ3roA&redirect_uri=https%3A%2F%2Foceandata.sci.gsfc.nasa.gov%2Fob%2Fgetfile%2Frestrict&response_type=code">Register</a>
</p>
<p class="form-instructions">
<em class="icon-question-sign"></em>
<a class="" href="/retrieve_info">I don’t remember my username</a>
<br /><em class="icon-question-sign"></em>
<a class="" href="/reset_passwords/new">I don’t remember my password</a>
<br />
<em class="icon-question-sign"></em>
<a href="javascript:feedback.showForm();" title = 'Need Help? Click on the Feedback button to request help'>Help</a>
</p>
</form>
<aside class="govt-msg">
<div class="nasa-logo"></div>
<p><strong>Why must I register?</strong></p>
<p>
The Earthdata Login provides a single mechanism for user registration and profile management for all EOSDIS system components (DAACs, Tools, Services).
Your Earthdata login also helps the EOSDIS program better understand the usage of EOSDIS services to improve user experience through customization of tools and improvement of services.
EOSDIS data are openly available to all and free of charge except where governed by international agreements.
</p>
</aside>
</section>
<section id="cta">
<h3>Get single sign-on access to all your favorite EOSDIS sites</h3>
<a class="eui-btn--round eui-btn--blue" href="/users/new?client_id=Z0u-MdLNypXBjiDREZ3roA&redirect_uri=https%3A%2F%2Foceandata.sci.gsfc.nasa.gov%2Fob%2Fgetfile%2Frestrict&response_type=code">Register for a Profile</a>
</section>
<div class="govt-warning eui-info-box">
<div class="warning-desktop">
<p><strong>By accessing and using this information system, you acknowledge and consent to the following:</strong></p>
You are accessing a U.S. Government information system, which includes:
(1) this computer;
(2) this computer network;
(3) all computers connected to this network including end user systems;
(4) all devices and storage media attached to this network or to any computer on this network; and
(5) cloud and remote information services. This information system is provided for U.S. Government-authorized use only.
You have no reasonable expectation of privacy regarding any communication transmitted through or data stored on this
information system. At any time, and for any lawful purpose, the U.S. Government may monitor, intercept, search, and
seize any communication or data transiting, stored on, or traveling to or from this information system. You are
NOT authorized to process classified information on this information system. Unauthorized or improper use of this
system may result in suspension or loss of access privileges, disciplinary action, and civil and/or criminal penalties.
</div>
<div class="warning-mobile">
<p><strong>By accessing and using this information system, you acknowledge and consent to the following:</strong></p>
You are accessing a U.S. Government information system, which includes:
(1) this computer;
(2) this computer network;
(3) all computers connected to this network including end user systems;
(4) all devices and storage media attached to this network or to any computer on this network; and
(5) cloud and remote information services. This information system is provided for U.S. Government-authorized use only.
Unauthorized or improper use of this system may result in suspension or loss of access privileges, disciplinary action,
and civil and/or criminal penalties. By using this information system, you acknowledge and consent to the terms
and conditions established in NASA policy and regulatory guidance for NASA IT Systems.
</div>
<div class="warning-mobile-mini">
<strong>
US Govt Property. Unauthorized use subject to prosecution. Use subject to monitoring per
NPD2810.
</strong>
</div>
</div>
</div>
<footer role="contentinfo">
<h3>For questions regarding the EOSDIS Earthdata Login, please contact Earthdata Support</h3>
<ul>
<li class="version badge eui-badge--md">V 4.158
</li>
<li>Home</li>
<li>Register</li>
<li>Documentation</li>
<li><a title="NASA Home" href="http://www.nasa.gov">NASA</a></li>
</ul>
<p>NASA Official: Stephen Berrick</p>
</footer>
<script src="/assets/application-15d4faf28e91715dccccf34f3e808b7a348298fb623e29d09281b30cc1f87492.js"></script>
<script type="text/javascript">
$(window).scroll(function(e){
parallax();
});
function parallax(){
var scrolled = $(window).scrollTop();
$('#content').css('background-position', 'right ' + -(scrolled*0.25)+'px ');
}
</script>
<script src="https://cdn.earthdata.nasa.gov/tophat2/tophat2.js" id="earthdata-tophat-script" data-show-fbm="true" data-show-status="true" data-status-api-url="https://status.earthdata.nasa.gov/api/v1/notifications"></script>
<script type="text/javascript" src="https://fbm.earthdata.nasa.gov/for/URS4/feedback.js"></script>
<script type="text/javascript">
feedback.init();
</script>
<script type="text/javascript">
setTimeout(function()
{var a=document.createElement("script"); var b=document.getElementsByTagName("script")[0];
a.src=document.location.protocol+"//dnn506yrbagrg.cloudfront.net/pages/scripts/0013/2090.js?"+Math.floor(new Date().getTime()/3600000);
a.async=true;a.type="text/javascript";b.parentNode.insertBefore(a,b)}
, 1);
</script>
<!-- BEGIN: DAP Google Analytics -->
<script language="javascript" id="_fed_an_ua_tag" src="https://dap.digitalgov.gov/Universal-Federated-Analytics-Min.js?agency=NASA&subagency=GSFC&dclink=true"></script>
<!-- END: DAP Google Analytics -->
</body>
</html>
I'm using the Python mandrill package 1.0.57, as a part of the mandrill add-on for my Heroku application. I attempt to add the following template:
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Test Title</title>
<!-- TODO styles -->
<style>
</style>
</head>
<body>
{{#each projects}}
<h3>{{this.name}}</h3>
<form>
<label for="log">Log</label>
<input type="number" id="log" name="log">
<input type="submit" value="Submit">
</form>
{{/each}}
</body>
</html>
The each loop and h3 work fine. However, I notice that upon adding the template, it has actually been modified. Specifically, the 'code' attribute of the template appears to be missing the form and input elements. The label is missing too, but its text, 'Log', is still present
This happens even if I place the elements outside of the loop. Are forms, inputs, and labels special?
Edit: I also directly tried a curl and it still modified my template.
curl -X POST -H "Content-Type: application/json" --data #test.json https://mandrillapp.com/api/1.0/templates/add.json -v
Edit 2: I guess a working workaround is to just use messages.send()
Say I have a web page like this on an Ubuntu 12.04 web server running apache:
<html>
<head>
<meta content="text/html; charset=ISO-8859-1"
http-equiv="content-type">
<title>Name Input</title>
</head>
<body>
<form action="./test.py" method="post">
<p> Name: <input type="text" name="name" id="name" value=""/></p>
<input type="submit" value="Submit" />
</form>
</body>
</html>
I want to use the value of name as input to a shell script which is called by a Python CGI script like this.
test.py:
#!/usr/bin/env python
import commands, cgi, cgitb
form = cgi.FieldStorage()
name = form.getvalue('name')
result = commands.getoutput("/usr/lib/cgi-bin/test.sh name")
contents = pageTemplate.format(**locals())
print "Content-type: text/html\n\n"
print contents
In the example code above, how should name be passed to test.sh?
For completeness, say that pageTemplate looks like this:
pageTemplate = '''<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta content="text/html; charset=ISO-8859-1"
http-equiv="content-type">
<title>Name Output</title>
</head>
<body>
{result}
</body>
</html>
'''
Just pass it into the command:
result = commands.getoutput("/usr/lib/cgi-bin/test.sh %s" % name)