File upload using httplib

Discussion in 'Python' started by alastair, Jul 26, 2004.

  1. alastair

    alastair Guest

    Hi,

    I'm attempting to test out some functionality of the Apache http
    server. What I'd like to do is send a file to the server - eg. a text
    file or binary file (I will be testing gzipped transfers eventually
    ....).

    At the moment I can test out sending a set of parameters to the
    server, and using mod_python, I have a python script which displays
    these values.

    I'm no expert when it comes to the http standard so I'm not sure if
    using httplib is the right way to go about things. If anyone has any
    advice on how to go about this, I'd appreciate it.

    Here's the code I've used to initiate a 'POST' to the server (straight
    out of the Python documentation):

    import httplib, urllib

    params = urllib.urlencode({'spam': 1, 'eggs': 2})

    headers = {"Content-type": "application/x-www-form-urlencoded",
    "Accept": "text/plain"}

    conn = httplib.HTTPConnection("127.0.0.1:8080")
    conn.request("POST", "/test/test.py")
    response = conn.getresponse()

    print response.status, response.reason

    data = response.read()
    conn.close()


    Cheers,

    Alastair.
    alastair, Jul 26, 2004
    #1
    1. Advertising

  2. alastair

    Peter Hansen Guest

    alastair wrote:

    > I'm attempting to test out some functionality of the Apache http
    > server.


    > I'm no expert when it comes to the http standard so I'm not sure if
    > using httplib is the right way to go about things. If anyone has any
    > advice on how to go about this, I'd appreciate it.


    What you are doing is mostly fine. Why not try it at the
    interactive interpreter prompt and experiment for yourself?
    This is Python: there's no excuse for not playing. :)

    > import httplib, urllib
    >
    > params = urllib.urlencode({'spam': 1, 'eggs': 2})
    >
    > headers = {"Content-type": "application/x-www-form-urlencoded",
    > "Accept": "text/plain"}
    >
    > conn = httplib.HTTPConnection("127.0.0.1:8080")
    > conn.request("POST", "/test/test.py")


    You don't use "headers" anywhere... note it should be a parameter
    to conn.request().

    At this point, though, you haven't really asked a question,
    and your code should mostly work (barring syntax errors etc,
    as I haven't tried it myself). Why not try it out and post
    questions if you have trouble and can't solve it yourself?

    -Peter
    Peter Hansen, Jul 26, 2004
    #2
    1. Advertising

  3. alastair

    John J. Lee Guest

    (alastair) writes:

    > I'm attempting to test out some functionality of the Apache http
    > server. What I'd like to do is send a file to the server - eg. a text
    > file or binary file (I will be testing gzipped transfers eventually
    > ...).


    This is called 'HTTP file upload'. Use Google Groups to see lots of
    posts about it in this newsgroup. Eg:

    http://groups.google.com/groups?&threadm=

    If you haven't done already, install a sniffer or a proxy. Ethereal
    works for me.

    [...]
    > headers = {"Content-type": "application/x-www-form-urlencoded",
    > "Accept": "text/plain"}
    >
    > conn = httplib.HTTPConnection("127.0.0.1:8080")
    > conn.request("POST", "/test/test.py")

    [...]

    As Peter said, you're failing to send the headers you think you are.

    If you did send those headers, though, you would be asking the server
    to believe you're POSTing application/x-www-form-urlencoded data
    (stuff like foo=bar&spam=eggs), but sending it a Python file.


    John
    John J. Lee, Jul 26, 2004
    #3
  4. alastair

    Jeff Shannon Guest

    alastair wrote:

    >Hi,
    >
    >I'm attempting to test out some functionality of the Apache http
    >server. What I'd like to do is send a file to the server - eg. a text
    >file or binary file (I will be testing gzipped transfers eventually
    >...).
    >


    Keep in mind that, in order to send a file, you must use a content type
    of multipart/form-data, rather than your current
    application/x-www-form-urlencoded. Neither urllib nor httplib directly
    support multipart/form-data, but (as someone else pointed out when I
    asked about this very recently) there's a recipe for it in ActiveState's
    Python Cookbook:

    http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/146306

    Note that the recipe uses an older httplib.HTTP() interface rather than
    the newer (and preferred) httplib.HTTPConnection() interface. However,
    the only change necessary to use the new interface (beyond changing the
    inital call) is to change the h.getreply() to h.getresponse(), and
    handle the HTTPResponse object properly.

    Jeff Shannon
    Technician/Programmer
    Credit International
    Jeff Shannon, Jul 27, 2004
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?U2FyYXY=?=

    Upload a file without file Upload control - ASP.Net

    =?Utf-8?B?U2FyYXY=?=, Aug 2, 2005, in forum: ASP .Net
    Replies:
    3
    Views:
    2,432
    Bruce Barker
    Aug 3, 2005
  2. John J. Lee

    Re: How to upload a file with httplib?

    John J. Lee, Jun 27, 2003, in forum: Python
    Replies:
    1
    Views:
    2,204
  3. Manish Jethani
    Replies:
    0
    Views:
    551
    Manish Jethani
    Dec 21, 2003
  4. Brian Beck
    Replies:
    1
    Views:
    353
    Brian Beck
    Oct 9, 2004
  5. Prakash
    Replies:
    3
    Views:
    449
    Ray at
    Nov 12, 2003
Loading...

Share This Page