[cherrypy-devel] using python to call cherrypost post

14 views
Skip to first unread message

juliaProcess

unread,
Apr 6, 2011, 3:02:37 PM4/6/11
to cherryp...@googlegroups.com

Hello
I would like to post and upload a file to my own cherrypy server, and so far
I am using a form to accomplish this.

----------------------------
CODE: SELECT ALL
<html><body>
<form action="upload" method="post" enctype="multipart/form-data">
filename: <input type="file" name="myFile" /><br />
<input type="submit" />
</form>
</body></html>

----------------------------

This works fine, however, sometimes, I need to run a separate python program
and tell it to upload another file instead of going through the html. I
think it it has something to do with urllib2, so this is what I tried:


---------------------------------
CODE: SELECT ALL
#!/usr/bin/python
import urllib
import urllib2

url = "http://localhost:5400/upload"

# this is POST data
values = {'type' : 'file', 'name' : "myfile.txt" }

data = urllib.urlencode(values)
req = urllib2.Request(url, data)
response = urllib2.urlopen(req)
the_page = response.read()

But I get this error
File "./test.py", line 13, in <module>
response = urllib2.urlopen(req)
File "/usr/lib/python2.6/urllib2.py", line 126, in urlopen
return _opener.open(url, data, timeout)
File "/usr/lib/python2.6/urllib2.py", line 397, in open
response = meth(req, response)
File "/usr/lib/python2.6/urllib2.py", line 510, in http_response
'http', request, response, code, msg, hdrs)
File "/usr/lib/python2.6/urllib2.py", line 435, in error
return self._call_chain(*args)
File "/usr/lib/python2.6/urllib2.py", line 369, in _call_chain
result = func(*args)
File "/usr/lib/python2.6/urllib2.py", line 518, in http_error_default
raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
urllib2.HTTPError: HTTP Error 400: Bad Request


I'm not exactly sure what is going on, any help would be appreciated.
--
View this message in context: http://old.nabble.com/using-python-to-call-cherrypost-post-tp31336238p31336238.html
Sent from the cherrypy-devel mailing list archive at Nabble.com.

Robert Brewer

unread,
Apr 7, 2011, 11:25:08 AM4/7/11
to cherryp...@googlegroups.com

juliaProcess

unread,
Apr 7, 2011, 12:50:04 PM4/7/11
to cherryp...@googlegroups.com


I already did a search on this before my post, and I did not find any
results that could help me solve the problem. It is probably due to my
understanding with the urllib module, and yes I read the documentation, and
I wrote the code base on my understanding.

Your search was no different from mine. I posted a very specific
problem, I posted my code, and I just wanted to know what I did wrong. I
would really appreciated it if anyone can point out my mistakes, that would
be helpful for me.

Just as an advice, being brusk and arrogant only makes a person look
ignorant.
--
View this message in context: http://old.nabble.com/using-python-to-call-cherrypost-post-tp31336238p31344301.html

juliaProcess

unread,
Apr 7, 2011, 1:15:11 PM4/7/11
to cherryp...@googlegroups.com

thank you, it works now
--
View this message in context: http://old.nabble.com/using-python-to-call-cherrypost-post-tp31336238p31344550.html
Reply all
Reply to author
Forward
0 new messages