I amd new to Tornado framework and trying to make a simple form to upload images:
<form method="post" action="/uploads/{{uid}}/" enctype="multipart/form-data" >
<input type="file" name="file1" /> <br />
Image info: <input type="text" name="alt" /> <br />
<input class="button" type="submit" value="Upload" class="button" />
</form>
I can successfully receive the Posted file using:
if 'file1' in self.request.files:
if self.request.files['imgfile'][0]:
file1 = self.request.files['imgfile'][0]
However I'm unable to receive the alt input. I tried alt = self.request.alt but I get this error
AttributeError: 'HTTPServerRequest' object has no attribute 'alt'
and when I use alt = self.request.files['alt'], I get:
KeyError: 'alt'
I ran out of ideas so appreciate your help.
UPDATE:
I found that this works:
alt = self.get_argument('alt')
But still open for better solutions.
Try code below
self.get_body_argument("alt", default=None, strip=False)
Related
I am having difficulty translating this specific HTML POST request into Python - I am attempting to exploit a security vulnerability on a test server.
director=
<form action="http://127.0.0.1:8000/card/0" method="POST">
<input type="hidden" name="amount" value="8000" />
<input type="hidden" name="username" value="hacker" />
<input type="submit" value="View my photos" />
</form>
Before you run the code below. Run pip install requests.
import requests
response = requests.get("http://api.open-notify.org/astros.json")
print(response)
>>>> Response<200>
See more details in the URL below.
https://www.nylas.com/blog/use-python-requests-module-rest-apis/
I already saw similar questions here describing same problem, peoples gives there answers and someones even respond it helped, but nothing from it totally works for me.
This is my code:
<html>
<body>
<form action = "/anomalydetector" enctype = "multipart-form-data" method = "post">
File: <input name = "attachment" type = "file" /><br />
Analyzer sensitivity in percents: <input type = "text" name = "sensitivity" value = "10" /><br />
<input type = "submit" value = "Analyze" />
</form>
</body>
</html>
and the handler:
class AnomalyDetectorPage(webapp2.RequestHandler):
def post(self):
uploaded_file = self.request.POST.get("attachment");
file_data = uploaded_file.file.read();
I always getting error of this kind:
File "/base/data/home/apps/s~test-ml/1.398533980585659886/main.py", line 207, in post
file_data = uploaded_file.file.read();
AttributeError: 'unicode' object has no attribute 'file'
I understand that python thinks that files are strings, but what I can do with it???
I tried self.request.POST.get("attachment").file.read(), self.request.POST["attachment"].file.read(), self.request.get("attachment").file.read() and self.request.POST.multi["attachment"].file.read() and maybe something else, but I always getting this error.
What I can do to read content of this file?
The enctype attribute value you are using is wrong. The form should be sent over:
multipart/form-data
<html>
<body>
<form action="/anomalydetector" enctype="multipart/form-data" method="post">
File: <input name="attachment" type="file" />
<br />
Analyzer sensitivity in percents: <input type="text" name="sensitivity" value="10" />
<br />
<input type="submit" value="Analyze" />
</form>
</body>
</html>
I've the following HTML code:
<html>
<form action="index.php" method="post">
Enter Input:
<input type="text" class="textbox" name="usn" id="usn" required />
<input class="buttongo" type="submit" value="Go" />
<input type="hidden" name="task" value="getResult"/>
</form>
</html>
I want to write a python script, which executes the above HTML, passing a value parameter to the first input statement to the above HTML. That is,
<html>
<form action="index.php" method="post">
Enter Input:
<input type="text" class="textbox" name="usn" id="usn" value="FromPython" />
<input class="buttongo" type="submit" value="Go" />
<input type="hidden" name="task" value="getResult"/>
</form>
</html>
Further, is there a way in which I can directly send the value to index.php and get the response?
(P.S.: I want to loop the value from 0 to 100 and save the response generated in a file)
Why don't you send the request using python ? You can send the requests inside a loop and pass the parameters you want.
Making requests with the requests module
sample code :
import requests
for i in range(101):
payload = {'usn': i}
response = requests.post("index.php", data=payload)
# do something with response
Use urllib module, documentation at: https://docs.python.org/2/library/urllib.html
As described in the link, this module provides a high-level interface for fetching data across the World Wide Web.
i am stuck on this issue since yesterday. i have a form where user can select more image files and submit. now i am not able to catch those POSTED image files in my views.py.
my html:
<input type="file" name="images[]" />
<input type="file" name="images[]" />
and in my views.py i did this:
images = request.POST.getlist('images[]')
but once i print images, i am getting [], empty array. what am i doing wrong? i want to get all selected image files and save them into db in my view.
EDIT:
this is my html:
<form action="/save/" method="post" enctype="multipart/form-data">
<input type="file" name="images[]" />
<input type="file" name="images[]" />
<input type="submit" value="send" />
</form>
this is my views.py
fotos = request.POST.getlist('images[]')
for i in range(len(fotos)):
print fotos[i]
here it is printing [], and not the names of each images. why is this array empty?
thanks for help.
When Django handles a file upload, it's a little different than a regular POST field. The data is place in request.FILES and needs to be accessed there. The documentation about handling file uploads is a great starting point.
Using python 3.2.3 I am trying to log into some websites and save the source from a hidden page to a file. I am stuck on how to log in. I have figured out how to log into a specific website but can't seem to get it working on others. The source of the webpage I am trying to log into is:
<form id="login_form" name="login_form" action="https://www.o2online.ie/amserver/UI/Login?org=o2ext&goto=%2F%2Fwww.o2online.ie%2Fo2%2Fmy-o2%2F" method="post">
<p id="form_header">My Account login</p>
<input value="Go" type="hidden" name="IDButton" id="IDButton"/>
<input value="o2ext" type="hidden" name="org" id="org"/>
<input value="TRUE" type="hidden" name="CONNECTFORMGET"/>
<label for="IDToken1">Username</label><br />
<input tabindex=1 type="text" id="IDToken1" name="IDToken1" value="Username/mobile" onclick="javascript: this.value='';" maxlength="60" onfocus="this.value='';" tabindex=1 /><br />
<br />
<label for="IDToken2">Password</label><br />
<input tabindex=2 type="password" id="IDToken2" name="IDToken2" value="" maxlength="30" onfocus="this.value='';" tabindex=2 />
<input tabindex=3 type="image" src="../images/my-o2/Login-button.png" id="submit_button" />
</form>
On the other pages that I have logged into sucessfully have had a submit button with a certain value but this webpage has an image for the submit button which has no value.
The code I have been using is:
import urllib.request
import urllib.parse
import http.client
import sys
url = 'http://www.o2online.ie/o2/login/'
login_data = {
'IDToken1': 'xxxx',
'IDToken2': 'xxxx',
#'submitbutton': 'submit'
}
# creating an opener object that will handle the cookies
opener = urllib.request.build_opener(urllib.request.HTTPCookieProcessor)
r = opener.open(url, urllib.parse.urlencode(login_data).encode())
# logged in
#Opening and saving source to a file
f = opener.open('http://www.o2online.ie/o2/my-o2/')
sys.stdout = open('file.html', 'w')
print (f.read(999999))
I have commented out the submit button.
Can this code be modified to log into this website?
How would you submit this form in python?