PYTHON HTTP POST

Discussion in 'Python' started by lilanidhaval@gmail.com, Jan 14, 2009.

  1. Guest

    Hi,

    I need one complete example of how to do a http post to any site.
    I have tried making a POST to google but all I am returned with is a
    405 error.
    I don't want to use Pygoogle as I want to try and do this with other
    sites.
    I am also having problems inputing with the param
    I have tried Mechanize. There are no problems with getting data only
    posting.

    >>> headers = {'Content-Type': 'text/html; charset=ISO-8859-1',

    .... 'User-Agent':'Mozilla/4.0',
    .... 'Content-Length':'7'}
    >>> conn = httplib.HTTPConnection("www.google.com")
    >>> conn.request("POST","/search",params,headers)
    >>> r2 = conn.getresponse()
    >>> print r2.status, r2.reason

    405 Method Not Allowed

    Regards,
    Dhaval
     
    , Jan 14, 2009
    #1
    1. Advertising

  2. Guest

    Does google accept POST?

    Anyways, if you dont need to post files, you can use urlencode itself.
    def encode_formdata(fields):
    body = urllib.urlencode(dict(<fields>))
    content_type = "application/x-www-form-urlencoded"
    return content_type, body

    If you need to post files too, then you will have to use multipart
    data
    def encode_multipart_formdata(fields, files):
    """
    fields is a sequence of (name, value) elements for regular form
    fields.
    files is a sequence of (name, filename, value) elements for data
    to be uploaded as files
    Return (content_type, body) ready for httplib.HTTP instance
    """
    BOUNDARY = '----------ThIs_Is_tHe_bouNdaRY_$'
    CRLF = '\r\n'
    L = []
    for (key, value) in fields:
    L.append('--' + BOUNDARY)
    L.append('Content-Disposition: form-data; name="%s"' % key)
    L.append('')
    L.append(value)
    for (key, filename, value) in files:
    L.append('--' + BOUNDARY)
    L.append('Content-Disposition: form-data; name="%s";
    filename="%s"' % (key, filename))
    L.append('Content-Type: %s' % mimetypes.guess_type(filename)
    [0] or 'application/octet-stream'
    L.append('')
    L.append(value)
    L.append('--' + BOUNDARY + '--')
    L.append('')
    body = CRLF.join(L)
    content_type = 'multipart/form-data; boundary=%s' % BOUNDARY
    return content_type, body

    Since POST files doesnt work with urllib, you might have to use
    httplib - or go for very high level tools like twisted.
    I here show an example with httplib.

    def post(host, selector, fields, files):
    if files:
    content_type, body = encode_multipart_formdata(fields, files)
    else:
    content_type, body = encode_formdata(fields)

    h = httplib.HTTPConnection(host)
    #Spoof Mozilla
    headers = {
    'User-Agent': 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US;
    rv:1.9.0.4) Gecko/2008102920 Firefox/3.0.4',
    'Content-Type': content_type
    }
    h.request('POST', selector, body, headers)
    res = h.getresponse()
    return res.status, res.reason, res.read()

    Please note that you can use multipart whether or not files are there,
    but parsing multipart usually is slower.

    Hope this helps.

    wrote:
    > Hi,
    >
    > I need one complete example of how to do a http post to any site.
    > I have tried making a POST to google but all I am returned with is a
    > 405 error.
    > I don't want to use Pygoogle as I want to try and do this with other
    > sites.
    > I am also having problems inputing with the param
    > I have tried Mechanize. There are no problems with getting data only
    > posting.
    >
    > >>> headers = {'Content-Type': 'text/html; charset=ISO-8859-1',

    > ... 'User-Agent':'Mozilla/4.0',
    > ... 'Content-Length':'7'}
    > >>> conn = httplib.HTTPConnection("www.google.com")
    > >>> conn.request("POST","/search",params,headers)
    > >>> r2 = conn.getresponse()
    > >>> print r2.status, r2.reason

    > 405 Method Not Allowed
    >
    > Regards,
    > Dhaval
     
    , Jan 14, 2009
    #2
    1. Advertising

  3. Guest

    On Jan 14, 2:21 pm, wrote:
    > Does google accept POST?
    >
    > Anyways, if you dont need to post files, you can use urlencode itself.
    > def encode_formdata(fields):
    >         body = urllib.urlencode(dict(<fields>))
    >         content_type = "application/x-www-form-urlencoded"
    >         return content_type, body
    >
    > If you need to post files too, then you will have to use multipart
    > data
    > def encode_multipart_formdata(fields, files):
    >     """
    >     fields is a sequence of (name, value) elements for regular form
    > fields.
    >     files is a sequence of (name, filename, value) elements for data
    > to be uploaded as files
    >     Return (content_type, body) ready for httplib.HTTP instance
    >     """
    >     BOUNDARY = '----------ThIs_Is_tHe_bouNdaRY_$'
    >     CRLF = '\r\n'
    >     L = []
    >     for (key, value) in fields:
    >         L.append('--' + BOUNDARY)
    >         L.append('Content-Disposition: form-data; name="%s"' % key)
    >         L.append('')
    >         L.append(value)
    >     for (key, filename, value) in files:
    >         L.append('--' + BOUNDARY)
    >         L.append('Content-Disposition: form-data; name="%s";
    > filename="%s"' % (key, filename))
    >         L.append('Content-Type: %s' % mimetypes.guess_type(filename)
    > [0] or 'application/octet-stream'
    >         L.append('')
    >         L.append(value)
    >     L.append('--' + BOUNDARY + '--')
    >     L.append('')
    >     body = CRLF.join(L)
    >     content_type = 'multipart/form-data; boundary=%s' % BOUNDARY
    >     return content_type, body
    >
    > Since POST files doesnt work with urllib, you might have to use
    > httplib - or go for very high level tools like twisted.
    > I here show an example with httplib.
    >
    > def post(host, selector, fields, files):
    >     if files:
    >        content_type, body = encode_multipart_formdata(fields, files)
    >     else:
    >        content_type, body = encode_formdata(fields)
    >
    >     h = httplib.HTTPConnection(host)
    >     #Spoof Mozilla
    >     headers = {
    >         'User-Agent': 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US;
    > rv:1.9.0.4) Gecko/2008102920 Firefox/3.0.4',
    >         'Content-Type': content_type
    >         }
    >     h.request('POST', selector, body, headers)
    >     res = h.getresponse()
    >     return res.status, res.reason, res.read()
    >
    > Please note that you can use multipart whether or not files are there,
    > but parsing multipart usually is slower.
    >
    > Hope this helps.
    >
    > wrote:
    > > Hi,

    >
    > > I need one complete example of how to do a http post to any site.
    > > I have tried making a POST to google but all I am returned with is a
    > > 405 error.
    > > I don't want to use Pygoogle as I want to try and do this with other
    > > sites.
    > > I am also having problems inputing with the param
    > > I have tried Mechanize. There are no problems with getting data only
    > > posting.

    >
    > > >>> headers = {'Content-Type': 'text/html; charset=ISO-8859-1',

    > > ...         'User-Agent':'Mozilla/4.0',
    > > ...         'Content-Length':'7'}
    > > >>> conn = httplib.HTTPConnection("www.google.com")
    > > >>> conn.request("POST","/search",params,headers)
    > > >>> r2 =  conn.getresponse()
    > > >>> print r2.status, r2.reason

    > > 405 Method Not Allowed

    >
    > > Regards,
    > > Dhaval

    >
    >


    oops - Forgot to mention that POSTing files mechanism is taken from a
    recipe in active state -
    http://code.activestate.com/recipes/146306/
     
    , Jan 14, 2009
    #3
  4. dhaval Guest

    The action part of the field is not set to anything.
    I need any site with working example that accepts POST.

    On Jan 14, 2:21 pm, wrote:
    > Does google accept POST?
    >
    > Anyways, if you dont need to post files, you can use urlencode itself.
    > def encode_formdata(fields):
    > body = urllib.urlencode(dict(<fields>))
    > content_type = "application/x-www-form-urlencoded"
    > return content_type, body
    >
    > If you need to post files too, then you will have to use multipart
    > data
    > def encode_multipart_formdata(fields, files):
    > """
    > fields is a sequence of (name, value) elements for regular form
    > fields.
    > files is a sequence of (name, filename, value) elements for data
    > to be uploaded as files
    > Return (content_type, body) ready for httplib.HTTP instance
    > """
    > BOUNDARY = '----------ThIs_Is_tHe_bouNdaRY_$'
    > CRLF = '\r\n'
    > L = []
    > for (key, value) in fields:
    > L.append('--' + BOUNDARY)
    > L.append('Content-Disposition: form-data; name="%s"' % key)
    > L.append('')
    > L.append(value)
    > for (key, filename, value) in files:
    > L.append('--' + BOUNDARY)
    > L.append('Content-Disposition: form-data; name="%s";
    > filename="%s"' % (key, filename))
    > L.append('Content-Type: %s' % mimetypes.guess_type(filename)
    > [0] or 'application/octet-stream'
    > L.append('')
    > L.append(value)
    > L.append('--' + BOUNDARY + '--')
    > L.append('')
    > body = CRLF.join(L)
    > content_type = 'multipart/form-data; boundary=%s' % BOUNDARY
    > return content_type, body
    >
    > Since POST files doesnt work with urllib, you might have to use
    > httplib - or go for very high level tools like twisted.
    > I here show an example with httplib.
    >
    > def post(host, selector, fields, files):
    > if files:
    > content_type, body = encode_multipart_formdata(fields, files)
    > else:
    > content_type, body = encode_formdata(fields)
    >
    > h = httplib.HTTPConnection(host)
    > #Spoof Mozilla
    > headers = {
    > 'User-Agent': 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US;
    > rv:1.9.0.4) Gecko/2008102920 Firefox/3.0.4',
    > 'Content-Type': content_type
    > }
    > h.request('POST', selector, body, headers)
    > res = h.getresponse()
    > return res.status, res.reason, res.read()
    >
    > Please note that you can use multipart whether or not files are there,
    > but parsing multipart usually is slower.
    >
    > Hope this helps.
    >
    > wrote:
    > > Hi,

    >
    > > I need one complete example of how to do a http post to any site.
    > > I have tried making a POST to google but all I am returned with is a
    > > 405 error.
    > > I don't want to use Pygoogle as I want to try and do this with other
    > > sites.
    > > I am also having problems inputing with the param
    > > I have tried Mechanize. There are no problems with getting data only
    > > posting.

    >
    > > >>> headers = {'Content-Type': 'text/html; charset=ISO-8859-1',

    > > ... 'User-Agent':'Mozilla/4.0',
    > > ... 'Content-Length':'7'}
    > > >>> conn = httplib.HTTPConnection("www.google.com")
    > > >>> conn.request("POST","/search",params,headers)
    > > >>> r2 = conn.getresponse()
    > > >>> print r2.status, r2.reason

    > > 405 Method Not Allowed

    >
    > > Regards,
    > > Dhaval
     
    dhaval, Jan 14, 2009
    #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. Replies:
    6
    Views:
    6,083
    Tor Iver Wilhelmsen
    Aug 29, 2005
  2. James
    Replies:
    3
    Views:
    16,640
    Roedy Green
    Nov 25, 2005
  3. ColinK
    Replies:
    0
    Views:
    595
    ColinK
    Jul 15, 2007
  4. serge calderara

    HTTP GET vs HTTP POST protocol

    serge calderara, Nov 4, 2005, in forum: ASP .Net Web Services
    Replies:
    1
    Views:
    665
  5. n3d!m

    Http post and http get

    n3d!m, Jan 25, 2012, in forum: Python
    Replies:
    2
    Views:
    394
    n3d!m
    Feb 6, 2012
Loading...

Share This Page