using python to call cherrypost post

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

using python to call cherrypost post

juliaProcess
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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

RE: using python to call cherrypost post

Robert Brewer-4
juliaProcess wrote:

> 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.

http://www.google.com/search?q=urllib2+multipart+form-data

http://stackoverflow.com/questions/680305/using-multipartposthandler-to-
post-form-data-with-python


Robert Brewer
[hidden email]

--
You received this message because you are subscribed to the Google Groups "cherrypy-devel" group.
To post to this group, send email to [hidden email].
To unsubscribe from this group, send email to [hidden email].
For more options, visit this group at http://groups.google.com/group/cherrypy-devel?hl=en.

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: using python to call cherrypost post

juliaProcess
In reply to this post by juliaProcess
thank you, it works now
Loading...