1

I am on a windows 10 laptop. When i manually open submit.html on my local computer click and browse to namo.jpg namo.png and then submit, i get the website processing my image and return with result file within 15 seconds. But I can't seem to get it to do the same using Python mechanize, when it run the script, the mechanize_results.html file keeps returning too quickly and telling me in their page that "Uploaded file is not a valid image. Only JPG, PNG and GIF files are allowed.. " Not sure what i have to change to get the site to recognize my file submitted by my python mechanize script as an image file.

my submit.html file has this

 <form name="myform" id="myform" action="http://deepdreamgenerator.com/upload-im" enctype="multipart/form-data" method="POST" id="upload-form">
    <input type="hidden" name="_token" value="pfC1a6HGVdbWO7mCmKVkqVinCkSYOKkQxXZV9NY1">
    <input type="file" name="file" id="upload"/>
    <input type="submit" />
</form>

my python mechanize script has this import mechanize

filename = 'C:/Users/tintran/Desktop/namo.png'
url = "file:///C:/Users/tintran/Desktop/submit.html"
br = mechanize.Browser()
br.set_handle_robots(False) # ignore robots
br.open(url)
br.select_form('myform')
br.set_all_readonly(False)
br.form.add_file(open(filename,'r'))
res = br.submit()
content = res.read()
with open("mechanize_results.html", "w") as f:
    f.write(content)

1 Answer 1

1

https://docs.python.org/2/library/functions.html#open

If mode is omitted, it defaults to 'r'. The default is to use text mode, which may convert '\n' characters to a platform-specific representation on writing and back on reading. Thus, when opening a binary file, you should append 'b' to the mode value to open the file in binary mode, which will improve portability. (Appending 'b' is useful even on systems that don’t treat binary and text files differently, where it serves as documentation.) See below for more possible values of mode.

It's all about Windows here. So just use 'rb' for opening PNG file.

Sign up to request clarification or add additional context in comments.

5 Comments

I just tried this and it still doesn't work, same error "Uploaded file is not a valid image. Only JPG, PNG and GIF files are allowed.. "
Are you sure you upload the same file manually and from python? You wrote that you upload namo.jpg manually. But in the code namo.png is used.
whoops typo...i don't even have a file called namo.jpg..sorry i'll edit the question right now.
last night, i had this br.form.add_file(open(filename),'images/png',filename,name='file') and it didn't work...
but just now i solved it using your 'rb' suggestion ...it work with br.form.add_file(open(filename,'rb'),'images/png',filename,name='file')

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.