requests avoid ddos protection [duplicate] - python

This question already has an answer here:
A specific site is returning a different response on python and in chrome
(1 answer)
Closed 4 years ago.
When I am trying to access this link from: browser
I have that reponse:
{"success":false}
But when I'm trying to call it from lib requests, I use that code:
import requests
s = requests.Session()
h = s.get('https://csgopolygon.com/scripts/_createAccount.php')
print(h.text)
When I'm running code response is:
<html>
<head>
<meta charset="UTF-8">
<script src="https://www.google.com/recaptcha/api.js" async defer></script>
<style>
html, body {
width: 100%;
border: 0px;
padding: 0px;
margin: 0px;
font-size: 18px;
}
.content {
width: 800px;
max-width: 95%;
margin: 0 auto;
opacity: 0;
transition: all 0.4s ease 2s;
}
.content.show {
opacity: 1;
}
.form {
position: relative;
}
.form > .lefCol {
display: inline-block;
width: 48%;
min-width: 300px;
padding: 0px;
vertical-align: top;
}
.form > .rightCol {
display: inline-block;
width: 48%;
min-width: 300px;
padding: 0px;
vertical-align: top;
}
.button {
font-size: 32px;
padding: 5px;
}
.footer {
padding: 15px;
text-align: right;
}
#loading {
text-align: center;
}
#fountainTextG{
width:420px;
margin:auto;
}
.fountainTextG{
color:rgb(0,0,0);
font-family:Arial;
font-size:44px;
text-decoration:none;
font-weight:normal;
font-style:normal;
float:left;
animation-name:bounce_fountainTextG;
-o-animation-name:bounce_fountainTextG;
-ms-animation-name:bounce_fountainTextG;
-webkit-animation-name:bounce_fountainTextG;
-moz-animation-name:bounce_fountainTextG;
animation-duration:2.09s;
-o-animation-duration:2.09s;
-ms-animation-duration:2.09s;
-webkit-animation-duration:2.09s;
-moz-animation-duration:2.09s;
animation-iteration-count:infinite;
-o-animation-iteration-count:infinite;
-ms-animation-iteration-count:infinite;
-webkit-animation-iteration-count:infinite;
-moz-animation-iteration-count:infinite;
animation-direction:normal;
-o-animation-direction:normal;
-ms-animation-direction:normal;
-webkit-animation-direction:normal;
-moz-animation-direction:normal;
transform:scale(.5);
-o-transform:scale(.5);
-ms-transform:scale(.5);
-webkit-transform:scale(.5);
-moz-transform:scale(.5);
}#fountainTextG_1{
animation-delay:0.75s;
-o-animation-delay:0.75s;
-ms-animation-delay:0.75s;
-webkit-animation-delay:0.75s;
-moz-animation-delay:0.75s;
}
#fountainTextG_2{
animation-delay:0.9s;
-o-animation-delay:0.9s;
-ms-animation-delay:0.9s;
-webkit-animation-delay:0.9s;
-moz-animation-delay:0.9s;
}
#fountainTextG_3{
animation-delay:1.05s;
-o-animation-delay:1.05s;
-ms-animation-delay:1.05s;
-webkit-animation-delay:1.05s;
-moz-animation-delay:1.05s;
}
#fountainTextG_4{
animation-delay:1.2s;
-o-animation-delay:1.2s;
-ms-animation-delay:1.2s;
-webkit-animation-delay:1.2s;
-moz-animation-delay:1.2s;
}
#fountainTextG_5{
animation-delay:1.35s;
-o-animation-delay:1.35s;
-ms-animation-delay:1.35s;
-webkit-animation-delay:1.35s;
-moz-animation-delay:1.35s;
}
#fountainTextG_6{
animation-delay:1.5s;
-o-animation-delay:1.5s;
-ms-animation-delay:1.5s;
-webkit-animation-delay:1.5s;
-moz-animation-delay:1.5s;
}
#fountainTextG_7{
animation-delay:1.64s;
-o-animation-delay:1.64s;
-ms-animation-delay:1.64s;
-webkit-animation-delay:1.64s;
-moz-animation-delay:1.64s;
}
#fountainTextG_8{
animation-delay:1.79s;
-o-animation-delay:1.79s;
-ms-animation-delay:1.79s;
-webkit-animation-delay:1.79s;
-moz-animation-delay:1.79s;
}
#fountainTextG_9{
animation-delay:1.94s;
-o-animation-delay:1.94s;
-ms-animation-delay:1.94s;
-webkit-animation-delay:1.94s;
-moz-animation-delay:1.94s;
}
#fountainTextG_10{
animation-delay:2.09s;
-o-animation-delay:2.09s;
-ms-animation-delay:2.09s;
-webkit-animation-delay:2.09s;
-moz-animation-delay:2.09s;
}
#keyframes bounce_fountainTextG{
0%{
transform:scale(1);
color:rgb(0,0,0);
}
100%{
transform:scale(.5);
color:rgb(255,255,255);
}
}
#-o-keyframes bounce_fountainTextG{
0%{
-o-transform:scale(1);
color:rgb(0,0,0);
}
100%{
-o-transform:scale(.5);
color:rgb(255,255,255);
}
}
#-ms-keyframes bounce_fountainTextG{
0%{
-ms-transform:scale(1);
color:rgb(0,0,0);
}
100%{
-ms-transform:scale(.5);
color:rgb(255,255,255);
}
}
#-webkit-keyframes bounce_fountainTextG{
0%{
-webkit-transform:scale(1);
color:rgb(0,0,0);
}
100%{
-webkit-transform:scale(.5);
color:rgb(255,255,255);
}
}
#-moz-keyframes bounce_fountainTextG{
0%{
-moz-transform:scale(1);
color:rgb(0,0,0);
}
100%{
-moz-transform:scale(.5);
color:rgb(255,255,255);
}
}
</style>
</head>
<body>
<div class="content" id="captcha">
<h1>Stormwall DDoS protection</h1>
<form class="form" action="" method="POST">
<div class="lefCol">
<input type="hidden" name="swp_sessionKey" value="${sessionKey}">
<br/>
<div class="g-recaptcha" data-sitekey="${recaptchaPublicKey}"></div>
<br/>
<input type="submit" class="button" value="Go to website">
</div>
<div class="rightCol">
<p>Please confirm you are not a robot.</p>
<p>Пожалуйста, подтвердите, что Вы не робот.</p>
</div>
</form>
<div class="footer">stormwall.pro</div>
</div>
<div class="content" id="loading">
<div id="fountainTextG"><div id="fountainTextG_1" class="fountainTextG">L</div><div id="fountainTextG_2" class="fountainTextG">o</div><div id="fountainTextG_3" class="fountainTextG">a</div><div id="fountainTextG_4" class="fountainTextG">d</div><div id="fountainTextG_5" class="fountainTextG">i</div><div id="fountainTextG_6" class="fountainTextG">n</div><div id="fountainTextG_7" class="fountainTextG">g</div><div id="fountainTextG_8" class="fountainTextG">.</div><div id="fountainTextG_9" class="fountainTextG">.</div><div id="fountainTextG_10" class="fountainTextG">.</div></div>
</div>
<iframe id="reportFrame" title="Report frame" type="text/html" style="width: 1px; height: 1px; display: none;">
<h1>Automatic report form</h1>
<form id="reportForm" action="http://reports.stormwall.pro" method="post">
<input type="text" name="userAgent" value="python-requests/2.19.1">
<input type="text" name="ipAddress" value="195.218.182.93">
<input type="text" name="host" value="csgopolygon.com">
</form>
</iframe>
<script type="text/javascript">
var timer1 = setTimeout(function(){
document.getElementById("captcha").className = "content show";
}, 10);
const cE = "386t98trrw9mjgo1gcd4jvkk9vae?!yt7u1031qg64h3rywryo5dahsgfszhg:ka8crny2n4o26v";
const cK = 2;
const cN = "swp_token";
const cO = ";path=/;max-age=1800";
try {
(function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
let avd = require('./avd');
let abc = '0123456789qwertyuiopasdfghjklzxcvbnm:?!';
let _rpct = [];
let _rRpct = {};
function gTb(abc) {
for(let idx=0; idx<abc.length; idx++ ) {
_rpct[idx] = abc[idx];
_rRpct[abc[idx]] = idx;
}
}
function csr(key, text) {
let mIn = _rpct.length - 1;
let rs = '';
for(let idx=0; idx<text.length; idx++) {
let sbl = text[idx];
if( typeof(_rRpct[sbl]) == 'undefined' ) {
rs = rs + sbl;
} else {
let nCD = _rRpct[sbl] + key;
if( nCD > mIn ) {
nCD = nCD - mIn - 1;
} else if( nCD < 0 ) {
nCD = mIn + nCD + 1;
}
rs = rs + _rpct[nCD];
}
}
return rs;
}
function vgE(sk, text) {
let mIn = _rpct.length - 1;
let crK = sk;
let rs = '';
for(let idx=0; idx<text.length; idx++) {
let sbr = '' + text[idx];
rs = rs + csr(crK, sbr);
crK = crK + 1;
if( crK > mIn ) {
crK = 0;
}
}
return rs;
}
function vgD(sk, text) {
let mIn = _rpct.length - 1;
let crK = sk;
let rs = '';
if( avd() )
rs += Date.new().toString();
for(let idx=0; idx<text.length; idx++) {
let sbr = '' + text[idx];
rs = rs + csr(crK * -1, sbr);
crK = crK + 1;
if( crK > mIn ) {
crK = 0;
}
}
return rs;
}
gTb(abc);
let dCC = vgD(cK, cE);
document.cookie = cN + '=' + dCC + cO;
window.location = window.location;
clearTimeout(timer1);
let captcha = document.getElementById('captcha');
captcha.parentNode.removeChild(captcha);
document.getElementById('loading').className = 'content show';
},{"./avd":2}],2:[function(require,module,exports){
var bcbs = {
a: window.callPhantom,
b: window._phantom,
c: window.__phantoma,
d: window.Buffer,
e: window.emit,
f: window.spawn,
g: window.webdriver,
h: window.domAutomation
};
function vd() {
for(var i in bcbs) {
if( typeof(bcbs[i]) != 'undefined' ) {
return true;
}
}
return false;
}
module.exports = vd;
},{}]},{},[1]);
} catch(err) {
var iframe = '<form id="reportForm" action="http://reports.stormwall.pro" method="POST"><input type="text" name="userAgent" value="${userAgent}"><input type="text" name="ipAddress" value="${ipAddress}"><input type="text" name="host" value="${host}"><input type="text" name="jsError" value="${jsError}"></form>';
iframe = iframe + '<' + 'script type="text/javascript"' + '>document.getElementById("reportForm").submit();<' +'/script>';
var doc = document.getElementById('reportFrame').contentWindow.document;
doc.open();
doc.write( iframe.replace('${jsError}', err) );
doc.close();
}
</script>
</body>
</html>
It's ddos protection. How to avoid it? Or how to send captcha ? Thanks for any help.

I should have used some the headers from my browser
import requests
s = requests.Session()
s.headers = {
'cookie': 'swp_token=1529318441:da348e5038f36f4e22e839d6e317852a:c8fe351689c07b18b38cf1bb7e6604ff',
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.87 Safari/537.36'
}
h = s.get('https://csgopolygon.com/scripts/_createAccount.php')
print(h.text)

Related

Django filter local folders

I have a templatetag function that returns a Collapse menu with all folders, subfolders and images in a root directory but now I want to be able to filter which folders I get, the templatetag function recives one parameter, the root directory, and I know it should recibe a second parameter with the folder I want to get but I'm confused in this part.
in my settings.py I have this:
STATICFILES_DIRS = [
BASE_DIR / 'static',
'D:/Users/my_user/Documents/Luis/results',
]
in my templatetag.py I have this:
from django import template
from django.utils.safestring import mark_safe
import os
register = template.Library()
#register.simple_tag
def my_tag(path):
html=''
for file in os.listdir(path):
rel = os.path.join(path, file)
if os.path.isdir(rel):
html += f"""<button type="button" class="collapsible">{file}</button>"""
html +='<div class="content"><div class="row mt-2">'
html += my_tag(rel)
html += '</div></div>'
else:
html +=f'<div class="col-md-6 d-flex align-items-stretch"><picture>'
x=str(rel)[43:]
print(x)
html += """<img src="/static"""+"""/%s" """%(x)
html += """ class="lazy card-img-top" width="500" height="400" """
html += """alt="/static"""+"""/%s" """%(x)
html +='></picture></div>'
return mark_safe(html)
and in my template.html I have this:
{% load static files %}
<style>
.collapsible {
background-color: #F8F8F8;
color: #858E9B;
cursor: pointer;
padding: 18px;
width: 100%;
border: none;
text-align: left;
outline: none;
font-size: 15px;
}
/* .active, .collapsible:hover {
background-color: #555;
}*/
.content {
padding: 0 18px;
display: none;
overflow: hidden;
background-color: #F8F8F8;
}.card-img-top {
height: 90vh !important;
object-fit: contain;
}
</style>
{% my_tag 'D:/Users/my_user/Documents/Luis/results' %}
<script>
var coll = document.getElementsByClassName("collapsible");
var i;
for (i = 0; i < coll.length; i++) {
coll[i].addEventListener("click", function() {
this.classList.toggle("active");
var content = this.nextElementSibling;
if (content.style.display === "block") {
content.style.display = "none";
} else {
content.style.display = "block";
}
});
}
</script>
This is an example of the final result:
So the idea is to get a specific day, can anyone give me an idea?

Need suggestions : Django UserProfile geographic address to connect with others users

I hope you're well,
I'm looking for a plugin or a tutorial for :
a- allow my users to fill in their address on their profile. The best would be to have a map on which they can identify their address or like on the UberEats site.
b- to be able to find the closest users according to their address.
If you have any ideas, I'm interested,
I have already made the profile on Django, all I need is the address field.
The below code will do. You will need Javascript to play around a bit and set your form fields with the returned address.
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no">
<meta charset="utf-8">
<style>
html,
body {
height: 100%;
margin: 0;
padding: 0;
}
#map {
height: 100%;
}
.controls {
margin-top: 10px;
border: 1px solid transparent;
border-radius: 2px 0 0 2px;
box-sizing: border-box;
-moz-box-sizing: border-box;
height: 32px;
outline: none;
box-shadow: 0 2px 6px rgba(0, 0, 0, 0.3);
}
#pac-input {
background-color: #fff;
font-family: Roboto;
font-size: 15px;
font-weight: 300;
margin-left: 12px;
padding: 0 11px 0 13px;
text-overflow: ellipsis;
width: 300px;
}
#pac-input:focus {
border-color: #4d90fe;
}
.pac-container {
font-family: Roboto;
}
#type-selector {
color: #fff;
background-color: #4d90fe;
padding: 5px 11px 0px 11px;
}
#type-selector label {
font-family: Roboto;
font-size: 13px;
font-weight: 300;
}
</style>
<title>Places Searchbox</title>
<style>
#target {
width: 345px;
}
</style>
</head>
<body>
<input id="pac-input" class="controls" type="text" placeholder="Search Box">
<div id="map"></div>
<script>
// This example adds a search box to a map, using the Google Place Autocomplete
// feature. People can enter geographical searches. The search box will return a
// pick list containing a mix of places and predicted search terms.
function initAutocomplete() {
var map = new google.maps.Map(document.getElementById('map'), {
center: { lat: -33.8688, lng: 151.2195 },
zoom: 13,
mapTypeId: google.maps.MapTypeId.ROADMAP
});
// Create the search box and link it to the UI element.
var input = document.getElementById('pac-input');
var searchBox = new google.maps.places.SearchBox(input);
map.controls[google.maps.ControlPosition.TOP_LEFT].push(input);
// Bias the SearchBox results towards current map's viewport.
map.addListener('bounds_changed', function () {
searchBox.setBounds(map.getBounds());
});
map.addListener('click', function (locationdata) {
console.log(locationdata.latLng.lat());
console.log(locationdata.latLng.lng());
var myLatlng = new google.maps.LatLng(locationdata.latLng.lat(), locationdata.latLng.lng());
var myOptions = {
zoom: 13,
center: myLatlng
}
});
var geocoder = new google.maps.Geocoder();
google.maps.event.addListener(map, 'click', function (event) {
geocoder.geocode({
'latLng': event.latLng
}, function (results, status) {
if (status == google.maps.GeocoderStatus.OK) {
if (results[0]) {
alert(results[0].formatted_address);
}
}
});
});
var markers = [];
// [START region_getplaces]
// Listen for the event fired when the user selects a prediction and retrieve
// more details for that place.
searchBox.addListener('places_changed', function () {
var places = searchBox.getPlaces();
if (places.length == 0) {
return;
}
// Clear out the old markers.
markers.forEach(function (marker) {
marker.setMap(null);
});
markers = [];
// For each place, get the icon, name and location.
var bounds = new google.maps.LatLngBounds();
places.forEach(function (place) {
console.log("lat: " + place.geometry.location.lat() + " lng: " + place.geometry.location.lng())
var icon = {
url: place.icon,
size: new google.maps.Size(71, 71),
origin: new google.maps.Point(0, 0),
anchor: new google.maps.Point(17, 34),
scaledSize: new google.maps.Size(25, 25)
};
// Create a marker for each place.
markers.push(new google.maps.Marker({
map: map,
icon: icon,
title: place.name,
position: place.geometry.location
}));
if (place.geometry.viewport) {
// Only geocodes have viewport.
bounds.union(place.geometry.viewport);
} else {
bounds.extend(place.geometry.location);
}
});
map.fitBounds(bounds);
});
// [END region_getplaces]
}
</script>
<script
src="https://maps.googleapis.com/maps/api/js?key=AIzaSyAkwr5rmzY9btU08sQlU9N0qfmo8YmE91Y&libraries=places&callback=initAutocomplete"
async defer></script>
</body>
</html>
You will need to add markers on map for each select. The code will console log the long and lat also, and return an alert with the address once you click it.
Credit goes to these two threads Link1 and Link2.
I managed to combine both codes and give results as you have mentioned.

How can I run a function in another thread until a button is pressed, and then how can I update the web page in that thread without JavaScript?

I am making a website that uses GPIO pins to read and display temperature and humidity. I have the basic functionality, but now I want what happens when you press the start button to run in another thread infinitely long, until you press the stop button. After each run the website should be updated. I work without JavaScript, but with flask.
This is what my website looks like. (Please do not pay attention to the If's)
.container
{
display: grid;
grid-template-columns: 33.33% 33.3% 33.3%;
grid-template-rows: 75px 75px 75px 75px 75px 75px 75px 75px 75px;
text-align: center;
}
#f1
{
grid-column: 1 / 4;
grid-row: 5 / 10;
display: grid;
grid-template-columns: 33.33% 33.33% 33.33%;
grid-template-rows: 75px 75px 75px 75px 75px;
text-align: center;
align-items: center;
}
.WhiteBackground
{
background: white;
}
.BlackColor
{
color: black;
}
.RedBackground
{
background: red;
}
.WhiteColor
{
color: white;
}
.BlueBackground
{
background: blue;
}
.GreenBackground
{
background: green;
}
.button {
font: 25px;
text-decoration: none;
background-color: #EEEEEE;
color: black;
padding: 2px 6px 2px 6px;
border-top: 1px solid #333333;
border-right: 1px solid #333333;
border-bottom: 1px solid #333333;
border-left: 1px solid #333333;
text-align: center;
line-height: 2em;
}
.fsize
{
font-size: 25px;
}
.cbtn
{
display: flex;
justify-content: center;
align-items: center;
}
#e6
{
grid-column: 1 / 2;
grid-row: 4 / 5;
}
#e7
{
grid-column: 3 / 4;
grid-row: 4 / 5;
}
#e8
{
grid-column: 2 / 3;
/*grid-row: 9 / 10;*/
grid-row: 5 / 6;
}
#h1Titel
{
grid-column: 1 / 4;
grid-row: 1 / 2;
text-align: center;
font-size: 43px;
}
#pTemperatur
{
grid-column: 1 / 2;
grid-row: 2 / 3;
text-align: center;
font-size: 25px;
}
#pLuftfeuchtigkeit
{
grid-column: 3 / 4;
grid-row: 2 / 3;
text-align: center;
font-size: 25px;
}
#AusgabeTemp
{
grid-column: 1 / 2;
grid-row: 3 / 4;
text-align: center;
font-size: 25px;
margin-block-start: 0em;
margin-block-end: 0em;
line-height: 70px;
vertical-align: middle;
}
#AusgabeLuft
{
grid-column: 3 / 4;
grid-row: 3 / 4;
text-align: center;
font-size: 25px;
vertical-align: middle;
margin-block-start: 0em;
margin-block-end: 0em;
line-height: 70px;
}
#buttonStart
{
grid-column: 1 / 2;
grid-row: 4 / 5;
text-align: center;
font-size: 25px;
width: 250px;
height: 50px;
}
#buttonStop
{
grid-column: 3 / 4;
grid-row: 4 / 5;
text-align: center;
font-size: 25px;
width: 250px;
height: 50px;
}
#pErklaerung1
{
grid-column: 2 / 3;
/*grid-row: 5 / 6;*/
grid-row: 1 / 2;
text-align: center;
font-size: 25px;
}
#TempGruenMin
{
grid-column: 1 / 2;
/*grid-row: 6 / 7;*/
grid-row: 2 / 3;
text-align: center;
font-size: 25px;
height: 75px;
}
#pUnd1
{
grid-column: 2 / 3;
/*grid-row: 6 / 7;*/
grid-row: 2 / 3;
text-align: center;
font-size: 25px;
}
#TempGruenMax
{
grid-column: 3 / 4;
/*grid-row: 6 / 7;*/
grid-row: 2 / 3;
text-align: center;
font-size: 25px;
height: 75px;
}
#pErklaerung2
{
grid-column: 2 / 3;
/*grid-row: 7 / 8;*/
grid-row: 3 / 4;
text-align: center;
font-size: 25px;
}
#LuftGruenMin
{
grid-column: 1 / 2;
/*grid-row: 8 / 9;*/
grid-row: 4 / 5;
text-align: center;
font-size: 25px;
height: 75px;
}
#pUnd2
{
grid-column: 2 / 3;
/*grid-row: 8 / 9;*/
grid-row: 4 / 5;
text-align: center;
font-size: 25px;
}
#LuftGruenMax
{
grid-column: 3 / 4;
/*grid-row: 8 / 9;*/
grid-row: 4 / 5;
text-align: center;
font-size: 25px;
height: 75px;
}
#buttonSpeichern
{
grid-column: 2 / 3;
/*grid-row: 9 / 10;*/
grid-row: 5 / 6;
text-align: center;
font-size: 25px;
width: 250px;
height: 50px;
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta charset="utf-8"/>
<title>Temperatur und Luftfeuchtigkeit</title>
<link rel="stylesheet" href="/static/css/main.css">
</head>
<body>
<div class="container">
<h1 id="h1Titel" style="font-size:40px">Temperatur und Luftfeuchtigkeit</h1>
<p id="pTemperatur" style="font-size:25px">Temperatur</p>
<p id="pLuftfeuchtigkeit" style="font-size:25px">Luftfeuchtigkeit</p>
{% if isRunning == False %}
<p id="AusgabeTemp" style="font-size:25px" class="WhiteBackground BlackColor">{{ temperature }}°C</p>
{% elif isTempBiggerMax == True %}
<p id="AusgabeTemp" style="font-size:25px" class="RedBackground WhiteColor">{{ temperature }}°C</p>
{% elif isTempLowerMin == True %}
<p id="AusgabeTemp" style="font-size:25px" class="BlueBackground WhiteColor">{{ temperature }}°C</p>
{% else %}
<p id="AusgabeTemp" style="font-size:25px" class="GreenBackground WhiteColor">{{ temperature }}°C</p>
{% endif %}
{% if isRunning == False %}
<p id="AusgabeLuft" style="font-size:25px" class="WhiteBackground BlackColor">{{ luftfeuchtigkeite }}%</p>
{% elif isLuftBiggerMax == True %}
<p id="AusgabeLuft" style="font-size:25px" class="RedBackground WhiteColor">{{ luftfeuchtigkeite }}%</p>
{% elif isLuftLowerMin == True %}
<p id="AusgabeLuft" style="font-size:25px" class="BlueBackground WhiteColor">{{ luftfeuchtigkeite }}%</p>
{% else %}
<p id="AusgabeLuft" style="font-size:25px" class="GreenBackground WhiteColor">{{ luftfeuchtigkeite }}%</p>
{% endif %}
{% if buttonStart == 'active' %}
<span class="cbtn" id="e6" style="font-size:25px">
Start
</span>
{% else %}
<span class="cbtn" id="e6" style="font-size:25px">
<a class="button" id="buttonStart" style="font-size:25px" disabled>Start</a>
</span>
{% endif %}
{% if buttonStop == 'active' %}
<span class="cbtn" id="e7" style="font-size:25px">
Stop
</span>
{% else %}
<span class="cbtn" id="e7" style="font-size:25px">
<a class="button" id="buttonStop" style="font-size:25px" disabled>Stop</a>
</span>
{% endif %}
<form id='f1' method="POST">
<p id="pErklaerung1" style="font-size:25px">Grüner Temperaturen Bereich: zwischen</p>
<input id="TempGruenMin" name="TempGruenMin" rows="1" value="{{ MinTemp }}">
<p id="pUnd1" style="font-size:25px">und</p>
<input id="TempGruenMax" name="TempGruenMax" rows="1" value="{{ MaxTemp }}">
<p id="pErklaerung2" style="font-size:25px">Grüner Luftfeuchtigkeiten Bereich: zwischen</p>
<input id="LuftGruenMin" name="LuftGruenMin" rows="1" value="{{ MinHumi }}">
<p id="pUnd2" style="font-size:25px">und</p>
<input id="LuftGruenMax" name="LuftGruenMax" rows="1" value="{{ MaxHumi }}">
<span class="cbtn" id="e8" style="font-size:25px">
<input type="submit" href="/speichern" value="Speichern" id="buttonSpeichern" style="font-size:25px">
</span>
</form>
</div>
</body>
</html>
My Python-File:
#!/usr/bin/env python
# coding: utf8
# -*- coding: utf-8 -*-
#imports
#variables and GPIO-Pins defined
#app.route('/')
#app.route('/index')
def index():
#Mainsite
#app.route('/<deviceName>')
def action(deviceName):
global Threadbeenden
global DHTSensor
global Luftfeuchtigkeite
global minimumGruenerBereichLuftfeuchtigkeit
global minimumGruenerBereichTemperatur
global maximalGruenerBereichLuftfeuchtigkeit
global maximalGruenerBereichTemperatur
global Temperature
global ledBluePin
global ledBlueStatus
global ledGreenPin
global ledGreenStatus
global ledRedPin
global ledRedStatus
global labLuftVordergrundFarbe
global labTempVordergrundFarbe
global labLuftHintergrundFarbe
global labTempHintergrundFarbe
global templateData
global isRunning
global isBiggerMax
global isLowerMin
global isLBiggerMax
global isLLowerMin
if deviceName == 'start':
isRunning = True
buttonStopStatus = ButtonEnabeln()
buttonStartStatus = ButtonDisabeln()
Luftfeuchtigkeit, Temperatur = dht.read_retry(DHTSensor, 23)
Luft = str(Luftfeuchtigkeit)
Temp = str(Temperatur)
ledRedStatus, ledBlueStatus, ledGreenStatus, labTempHintergrundFarbe, labTempVordergrundFarbe, labLuftHintergrundFarbe, labLuftVordergrundFarbe, isBiggerMax, isLowerMin, isLBiggerMax, isLLowerMin = Farbe(Temperatur, Luftfeuchtigkeit)
Luftfeuchtigkeite = Luft
Temperature = Temp
templateData = {
'ledRed' : ledRedStatus,
'ledBlue' : ledBlueStatus,
'ledGreen' : ledGreenStatus,
'buttonStart' : buttonStartStatus,
'buttonStop' : buttonStopStatus,
'temperature' : Temperature,
'luftfeuchtigkeite' : Luftfeuchtigkeite,
'MinTemp' : minimumGruenerBereichTemperatur,
'MaxTemp' : maximalGruenerBereichTemperatur,
'MinHumi' : minimumGruenerBereichLuftfeuchtigkeit,
'MaxHumi' : maximalGruenerBereichLuftfeuchtigkeit,
'isRunning' : isRunning,
'isTempBiggerMax' : isBiggerMax,
'isTempLowerMin' : isLowerMin,
'isLuftBiggerMax' : isLBiggerMax,
'isLuftLowerMin' : isLLowerMin
}
render_template('index.html', title='Home', **templateData)
if deviceName == 'stop':
isRunning = False
Threadbeenden = True
ledRedStatus = 0
ledBlueStatus = 0
ledGreenStatus = 0
buttonStartStatus = ButtonEnabeln()
buttonStopStatus = ButtonDisabeln()
Temperatur = '--'
Luftfeuchtigkeit = ''
labLuftHintergrundFarbe = 'WHITE'
labLuftVordergrundFarbe = 'BLACK'
labTempHintergrundFarbe = 'WHITE'
labTempVordergrundFarbe = 'BLACK'
GPIO.output(ledGreenPin, 0)
GPIO.output(ledBluePin, 0)
GPIO.output(ledRedPin, 0)
templateData = {
'ledRed' : ledRedStatus,
'ledBlue' : ledBlueStatus,
'ledGreen' : ledGreenStatus,
'buttonStart' : buttonStartStatus,
'buttonStop' : buttonStopStatus,
'temperature' : Temperature,
'luftfeuchtigkeite' : Luftfeuchtigkeite,
'MinTemp' : minimumGruenerBereichTemperatur,
'MaxTemp' : maximalGruenerBereichTemperatur,
'MinHumi' : minimumGruenerBereichLuftfeuchtigkeit,
'MaxHumi' : maximalGruenerBereichLuftfeuchtigkeit,
'isRunning' : isRunning,
'isTempBiggerMax' : isBiggerMax,
'isTempLowerMin' : isLowerMin,
'isLuftBiggerMax' : isLBiggerMax,
'isLuftLowerMin' : isLLowerMin
}
return render_template('index.html', title='Home', **templateData)
return render_template('index.html', title='Home', **templateData)
def Farbe(Temperatur, Luftfeuchtigkeit):
global minimumGruenerBereichLuftfeuchtigkeit
global minimumGruenerBereichTemperatur
global maximalGruenerBereichLuftfeuchtigkeit
global maximalGruenerBereichTemperatur
global labLuftHintergrundFarbe
global labLuftVordergrundFarbe
global labTempHintergrundFarbe
global labTempVordergrundFarbe
global isRunning
global isBiggerMax
global isLowerMin
global isLBiggerMax
global isLLowerMin
if Temperatur is not None and Luftfeuchtigkeit is not None:
print("Hallo")
if int(float(Temperatur)) < int(minimumGruenerBereichTemperatur):
labTempHintergrundFarbe = 'BLUE'
labTempVordergrundFarbe = 'WHITE'
GPIO.output(ledGreenPin, 0)
ledGreenStatus = 0
GPIO.output(ledBluePin, 1)
ledBlueStatus = 1
GPIO.output(ledRedPin, 0)
ledRedStatus = 0
isBiggerMax = False
isLowerMin = True
elif (int(float(Temperatur)) >= int(minimumGruenerBereichTemperatur)) and (int(float(Temperatur)) <= int(maximalGruenerBereichTemperatur)):
labTempHintergrundFarbe = 'GREEN'
labTempVordergrundFarbe = 'WHITE'
GPIO.output(ledGreenPin, 1)
ledGreenStatus = 1
GPIO.output(ledBluePin, 0)
ledBlueStatus = 0
GPIO.output(ledRedPin, 0)
ledRedStatus = 0
isBiggerMax = False
isLowerMin = False
else:
labTempHintergrundFarbe = 'RED'
labTempVordergrundFarbe = 'WHITE'
GPIO.output(ledGreenPin, 0)
ledGreenStatus = 0
GPIO.output(ledBluePin, 0)
ledBlueStatus = 0
GPIO.output(ledRedPin, 1)
ledRedStatus = 1
isBiggerMax = True
isLowerMin = False
if int(float(Luftfeuchtigkeit)) < int(minimumGruenerBereichLuftfeuchtigkeit):
labLuftHintergrundFarbe = 'BLUE'
labLuftVordergrundFarbe = 'WHITE'
isLBiggerMax = False
isLLowerMin = True
elif (int(float(Luftfeuchtigkeit)) >= int(minimumGruenerBereichLuftfeuchtigkeit)) and (int(float(Luftfeuchtigkeit)) <= int(maximalGruenerBereichLuftfeuchtigkeit)):
labLuftHintergrundFarbe = 'GREEN'
labLuftVordergrundFarbe = 'WHITE'
isLBiggerMax = False
isLLowerMin = False
else:
labLuftHintergrundFarbe = 'RED'
labLuftVordergrundFarbe = 'WHITE'
isLBiggerMax = True
isLLowerMin = False
return ledRedStatus, ledBlueStatus, ledGreenStatus, labTempHintergrundFarbe, labTempVordergrundFarbe, labLuftHintergrundFarbe, labLuftVordergrundFarbe, isBiggerMax, isLowerMin, isLBiggerMax, isLLowerMin
def ButtonEnabeln():
return 'active'
def ButtonDisabeln():
return 'disable'
#app.route('/<a>', methods=['POST'])
def speichern(a):
#savebutton clicked event
#app.route('/', methods=['POST'])
def save():
#savebutton clicked event
You can't do client side processing with python via jinja. The template is processed server side and rendered to the client browser. You would need to use JavaScript.
EDIT: You could make an AJAX request (via JavaScript) back to the server to do the processing, then update the client.

how to scrape an aspx rendered page using python

I am scraping an aspx rendered web page Link to Page
the website is .aspx, i chose Selenium , mechanize , urllib , lxml , Beautiful soup , requests . any insights/recommendations on coding the next steps. Also used scrapy.
I have used requests :
import requests
from bs4 import BeautifulSoup
request.get(url_to_page)
print request.content
it gives
<!DOCTYPE html>
<html>
<head><meta charset="utf-8" /><title>
www.tournamentsoftware.com
</title>
<style>
body {
font: normal 12px/20px Arial, Helvetica, sans-serif;
color: #505050;
background: #ccc url(//static.tournamentsoftware.com/images/cw_bg.png) repeat-x;
}
h2 {
font: bold 16px/16px Arial, Helvetica, sans-serif !important;
color: #000;
margin: 4px 0;
}
h4 {
font: bold 13px/13px Arial, Helvetica, sans-serif !important;
margin: 0 0 -8px 0;
}
p {
font: normal 12px/20px Arial, Helvetica, sans-serif;
margin: 12px 0;
}
p.note {
font: normal 10px/10px Arial, Helvetica, sans-serif;
margin: 8px 0 0 0;
text-align: center;
color: #999;
}
p.note.error {
font: bold 13px/20px Arial, Helvetica, sans-serif;
color: #f00;
}
.langtoggle { display:inline; margin-right:6px; }
.langtoggle.active { display:none; }
.langmessage { display:none; margin-bottom:20px; }
.langmessage.active { display:block; }
input.button {
margin: 4px 0;
}
</style>
</head>
<body>
<form method="post" action="./default.aspx?returnurl=%2fsport%2fdraw.aspx%3fid%3dE880C7A5-0A60-4A98-8FF9-A3B7DD58F3E2%26draw%3d4" id="form1" class="lang1033">
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="p4eGoAC3005ctvGuhkv1w6Nanrs87p7iDcl4Hlk1SNw/cJovTDsJZeq54VdP4JR0injIJb59okjgeTpi30pz0LH9qjU=" />
<input type="hidden" name="__VIEWSTATEGENERATOR" id="__VIEWSTATEGENERATOR" value="A86F2231" />
<div id="messagewindow">
<p class="toggles"><a id="Lang1033" class="lang langtoggle active" href="#" onclick="switchLang(this)">English</a> </p><div id="divLang1033" class="langmessage active"><h2>The use of cookies on www.tournamentsoftware.com</h2><p>We are legally obliged to get your
elems = document.getElementsByClassName('langmessage');
for (var i = 0; i < elems.length; i++) {
elems[i].className = 'langmessage';
}
document.getElementById(AThis.id).className = 'langtoggle active';
document.getElementById('div' + AThis.id).className = 'langmessage active';
return false;
}
function toggleCookiesHelp(AElmID) {
document.getElementById(AElmID).style.display = 'block';
return false;
}
function toggleCookiesHelpByClassName() {
var elems = document.getElementsByClassName('removecookies');
for (var i = 0; i < elems.length; i++) {
elems[i].style.display = 'block';
}
elems = document.getElementsByClassName('note');
for (var i = 0; i < elems.length; i++) {
elems[i].className = 'note error';
}
return false;
}
if (storageAvailable()) {
if (localStorage.getItem('cookiewall')) {
toggleCookiesHelpByClassName();
}
var elems = document.getElementsByClassName('button');
for (var i = 0; i < elems.length; i++) {
elems[i].addEventListener('click', function (e) {
localStorage.setItem('cookiewall', '1');
});
}
}
function storageAvailable() {
try {
var x = '__storage_test__';
localStorage.setItem(x, x);
localStorage.removeItem(x);
return true;
} catch(e) {
return false;
}
}
</script>
</form>
</body>
</html>
Also tried with mechanize, scrapy. All of them are giving this result only. How to scrape those websites. But I am able to see the source code in the browser. Is there any way to scrape those data.
import requests
from bs4 import BeautifulSoup
r_obj = requests.Session()
url = "http://www.tournamentsoftware.com/cookie/default.aspx?returnurl=%2fdefault.aspx"
fr_soup = r_obj.get(url)
soup = BeautifulSoup(fr_soup.content , "lxml")
#print soup
l = soup.find_all("input",type="hidden")
#print l
data = {
l[0]['name']:l[0]['value'],
l[1]['name']:l[1]['value'],
'btnAccept':'Yes, I accept'}
r_obj.post(url,verify=False,data=data)
url_needed = "http://www.tournamentsoftware.com/sport/draw.aspx?id=E880C7A5-0A60-4A98-8FF9-A3B7DD58F3E2&draw=4"
final = r_obj.get(url_needed)
#print final.content
soup1 = BeautifulSoup(final.content,"lxml")
detail_tab = soup1.find_all("table")
You need to use a framework which runs the client-side code for you. headless-chrome is one such tool.

Django error's when I try to view webpage source

I'm using Django to create a web app for a class. The snippet of HTML below is what I think might be causing my problem.
<form action="press" method="POST">
<table>
<tr>
<td colspan="4">
{% if output %}
<input id="output" name="output" type="text" value="{{output}}" readonly>
{% else %}
<input id="output_hidden" name="output" type="text" value="0" readonly>
{% endif %}
<input type="hidden" name="buttVal" value="{{buttVal}}">
<input type="hidden" name="currVal" value="{{currVal}}">
<input type="hidden" name="prevVal" value="{{prevVal}}">
<input type="hidden" name="math" value="{{math}}">
{% csrf_token %}
</td>
I'm using hidden fields to store values to keep the session stateless (point of the assignment). I'm trying to debug my hidden fields, however, after I start my app and trigger the first if statement below, I'm no longer able to view the webpage source (by right clicking in the webpage and clicking on "view source"). When I attempt this, it first says "Document Expired" but it allows me to try the request again. When I try it again, the html below shows up as source.
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<meta name="robots" content="NONE,NOARCHIVE">
<title>MultiValueDictKeyError at /press</title>
<style type="text/css">
html * { padding:0; margin:0; }
body * { padding:10px 20px; }
body * * { padding:0; }
body { font:small sans-serif; }
body>div { border-bottom:1px solid #ddd; }
h1 { font-weight:normal; }
h2 { margin-bottom:.8em; }
h2 span { font-size:80%; color:#666; font-weight:normal; }
h3 { margin:1em 0 .5em 0; }
h4 { margin:0 0 .5em 0; font-weight: normal; }
code, pre { font-size: 100%; white-space: pre-wrap; }
table { border:1px solid #ccc; border-collapse: collapse; width:100%; background:white; }
tbody td, tbody th { vertical-align:top; padding:2px 3px; }
thead th { padding:1px 6px 1px 3px; background:#fefefe; text-align:left; font-weight:normal; font-size:11px; border:1px solid #ddd; }
tbody th { width:12em; text-align:right; color:#666; padding-right:.5em; }
table.vars { margin:5px 0 2px 40px; }
table.vars td, table.req td { font-family:monospace; }
table td.code { width:100%; }
table td.code pre { overflow:hidden; }
table.source th { color:#666; }
table.source td { font-family:monospace; white-space:pre; border-bottom:1px solid #eee; }
ul.traceback { list-style-type:none; color: #222; }
ul.traceback li.frame { padding-bottom:1em; color:#666; }
ul.traceback li.user { background-color:#e0e0e0; color:#000 }
div.context { padding:10px 0; overflow:hidden; }
div.context ol { padding-left:30px; margin:0 10px; list-style-position: inside; }
div.context ol li { font-family:monospace; white-space:pre; color:#777; cursor:pointer; }
div.context ol li pre { display:inline; }
div.context ol.context-line li { color:#505050; background-color:#dfdfdf; }
div.context ol.context-line li span { position:absolute; right:32px; }
.user div.context ol.context-line li { background-color:#bbb; color:#000; }
.user div.context ol li { color:#666; }
div.commands { margin-left: 40px; }
div.commands a { color:#555; text-decoration:none; }
.user div.commands a { color: black; }
#summary { background: #ffc; }
#summary h2 { font-weight: normal; color: #666; }
#explanation { background:#eee; }
#template, #template-not-exist { background:#f6f6f6; }
#template-not-exist ul { margin: 0 0 0 20px; }
#unicode-hint { background:#eee; }
#traceback { background:#eee; }
#requestinfo { background:#f6f6f6; padding-left:120px; }
#summary table { border:none; background:transparent; }
#requestinfo h2, #requestinfo h3 { position:relative; margin-left:-100px; }
#requestinfo h3 { margin-bottom:-1em; }
.error { background: #ffc; }
.specific { color:#cc3300; font-weight:bold; }
h2 span.commands { font-size:.7em;}
span.commands a:link {color:#5E5694;}
pre.exception_value { font-family: sans-serif; color: #666; font-size: 1.5em; margin: 10px 0 10px 0; }
</style>
<script type="text/javascript">
//<!--
function getElementsByClassName(oElm, strTagName, strClassName){
// Written by Jonathan Snook, http://www.snook.ca/jon; Add-ons by Robert Nyman, http://www.robertnyman.com
var arrElements = (strTagName == "*" && document.all)? document.all :
oElm.getElementsByTagName(strTagName);
var arrReturnElements = new Array();
strClassName = strClassName.replace(/\-/g, "\-");
var oRegExp = new RegExp("(^|\s)" + strClassName + "(\s|$)");
var oElement;
for(var i=0; i<arrElements.length; i++){
oElement = arrElements[i];
if(oRegExp.test(oElement.className)){
arrReturnElements.push(oElement);
}
}
return (arrReturnElements)
}
function hideAll(elems) {
for (var e = 0; e < elems.length; e++) {
elems[e].style.display = 'none';
}
}
window.onload = function() {
hideAll(getElementsByClassName(document, 'table', 'vars'));
hideAll(getElementsByClassName(document, 'ol', 'pre-context'));
hideAll(getElementsByClassName(document, 'ol', 'post-context'));
hideAll(getElementsByClassName(document, 'div', 'pastebin'));
}
function toggle() {
for (var i = 0; i < arguments.length; i++) {
var e = document.getElementById(arguments[i]);
if (e) {
e.style.display = e.style.display == 'none' ? 'block': 'none';
}
}
return false;
}
function varToggle(link, id) {
toggle('v' + id);
var s = link.getElementsByTagName('span')[0];
var uarr = String.fromCharCode(0x25b6);
var darr = String.fromCharCode(0x25bc);
s.innerHTML = s.innerHTML == uarr ? darr : uarr;
return false;
}
function switchPastebinFriendly(link) {
s1 = "Switch to copy-and-paste view";
s2 = "Switch back to interactive view";
link.innerHTML = link.innerHTML == s1 ? s2: s1;
toggle('browserTraceback', 'pastebinTraceback');
return false;
}
//-->
Outside of this, I'm not really sure what's going on. I tried googling the problem, but my lack of understanding of this situation kept me from finding anything useful. Hopefully someone can point out something that I'm not doing correctly.
-- EDIT --
Here's my views.py file.
from django.shortcuts import render
from django.http import HttpResponse
# Create your views here.
def home_page(request):
index = 'calculator/calculator.html'
return render(request,index,{})
def press(request):
context = {}
operators = ['plus','minus','divide','multiply']
index = 'calculator/calculator.html'
## Discerns from a number and an operator press
if 'val' in request.POST and is_int(request.POST['val']):
if request.POST['output'] == '0':
context['output'] = request.POST['val']
context['buttVal'] = request.POST['val']
context['currVal'] = request.POST['val']
elif request.POST['currVal'] == '0':
context['output'] = request.POST['val']
context['buttVal'] = request.POST['val']
context['currVal'] = request.POST['val']
else:
newOutput = request.POST['output'] + request.POST['val']
context['buttVal'] = request.POST['val']
context['currVal'] = newOutput
context['output'] = newOutput
context['math'] = request.POST['math']
context['prevVal'] = request.POST['prevVal']
elif request.POST['val'] == 'clear':
context['output'] = 0
context['buttVal'] = 0
context['currVal'] = 0
context['prevVal'] = 0
context['math'] = ''
elif request.POST['val'] in operators:
print request.POST
if request.POST['math'] != '':
print "poop"
if request.POST['math'] == 'plus':
result = int(request.POST['currVal']) + int(request.POST['prevVal'])
context['prevVal'] = result
context['output'] = result
if request.POST['math'] == 'minus':
result = int(request.POST['currVal']) - int(request.POST['prevVal'])
context['prevVal'] = result
context['output'] = result
if request.POST['math'] == 'multiply':
result = int(request.POST['currVal']) * int(request.POST['prevVal'])
context['prevVal'] = result
context['output'] = result
if request.POST['math'] == 'divide':
result = int(request.POST['currVal']) / int(request.POST['prevVal'])
context['prevVal'] = result
context['output'] = result
if request.POST['val'] == 'equal':
pass
else:
context['prevVal'] = request.POST['currVal']
#context['math'] = request.POST['val']
context['output'] = request.POST['output']
context['math'] = request.POST['val']
context['buttVal'] = request.POST['buttVal']
context['currVal'] = 0
else:
context['output'] = request.POST['output']
context['buttVal'] = request.POST['buttVal']
context['math'] = request.POST['math']
context['prevVal'] = request.POST['prevVal']
## Submits POST
return render(request, index, context)
## Helper functions
def is_int(string):
try:
int(string)
return True
except ValueError:
return False
Your are trying to access to a parameters that are not exist in the request.POST. For example:
elif request.POST['val'] == 'clear':
...
elif request.POST['val'] in operators:
...
else:
context['output'] = request.POST['output']
context['buttVal'] = request.POST['buttVal']
context['math'] = request.POST['math']
context['prevVal'] = request.POST['prevVal']
You didn't check if val, output, buttVal, prevVal and math are in the request.POST.
To safely get values from the request.POST use the get() method:
request.POST.get('val')
This expression will return the val parameter of the request.POST or None if such parameter doesn't exist.
Add the print request.POST and the very beginning of the press() view to see the content of the POST data in the "view source" request.
This happens, because when you view source of a page in a browser, another request is made; but its a GET request.
Thus, your POST dictionary doesn't have those keys, resulting in the exception.
The easiest way to solve this problem is to use forms.
class MyForm(forms.Form):
output = forms.TextField()
# .. your other fields here
def press(request):
form = MyForm(request.POST or None)
if form.is_valid():
# do stuff
return redirect('/home')
return render(request, 'template.html', {'form': form})
You can read more about forms (including, how to hide fields and pass initial values) at the documentation.

Categories

Resources