Cannot get POST to work

Discussion in 'Python' started by tedpottel@gmail.com, Sep 29, 2009.

  1. Guest

    Hi,
    I'm trying to post data to a short test script in php I wrote.
    The python code to do the post is
    import httplib

    #server address
    conn = httplib.HTTPConnection("localhost")

    #file location
    conn.request("POST", "/programming/bots/test.php","&ted=fred")
    r1 = conn.getresponse()
    print r1.status, r1.reason
    data1 = r1.read()
    print data1
    conn.close()
    print "new ok"

    The PHP script is
    print"hello <br>";
    print $_POST["ted"];

    Ted post
     
    , Sep 29, 2009
    #1
    1. Advertising

  2. Falcolas Guest

    On Sep 29, 10:24 am, "" <>
    wrote:
    > Hi,
    > I'm trying to post data to a short test script in php I wrote.
    > The python code to do the post is
    > import httplib
    >
    > #server address
    > conn = httplib.HTTPConnection("localhost")
    >
    > #file location
    > conn.request("POST", "/programming/bots/test.php","&ted=fred")
    > r1 = conn.getresponse()
    > print r1.status, r1.reason
    > data1 = r1.read()
    > print data1
    > conn.close()
    > print "new ok"
    >
    > The PHP script is
    > print"hello <br>";
    > print $_POST["ted"];
    >
    > Ted post


    I can't speak to what is wrong with your current script - instead I
    would recommend the higher level urllib libraries:

    (Untested)
    import urllib2, urllib

    response = urllib2.open("http://localhost/programming/bots/test.php",
    urllib.urlencode({"ted": "fred"}))
    print response.read()
    response.close()

    ~Garrick
     
    Falcolas, Sep 29, 2009
    #2
    1. Advertising

  3. >>>>> "" <> (t) wrote:

    >t> Hi,
    >t> I'm trying to post data to a short test script in php I wrote.
    >t> The python code to do the post is
    >t> import httplib


    >t> #server address
    >t> conn = httplib.HTTPConnection("localhost")


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

    >t> #file location
    >t> conn.request("POST", "/programming/bots/test.php","&ted=fred")


    conn.request("POST", "/programming/bots/test.php", "ted=fred", headers)

    >t> r1 = conn.getresponse()
    >t> print r1.status, r1.reason
    >t> data1 = r1.read()
    >t> print data1
    >t> conn.close()
    >t> print "new ok"


    >t> The PHP script is
    >t> print"hello <br>";
    >t> print $_POST["ted"];


    >t> Ted post


    --
    Piet van Oostrum <>
    WWW: http://pietvanoostrum.com/
    PGP key: [8DAE142BE17999C4]
     
    Piet van Oostrum, Sep 30, 2009
    #3
    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. Edward
    Replies:
    0
    Views:
    553
    Edward
    Jul 28, 2003
  2. sianan
    Replies:
    1
    Views:
    6,423
    S. Justin Gengo
    Nov 8, 2005
  3. arnuld
    Replies:
    6
    Views:
    651
    arnuld
    Sep 5, 2007
  4. C Jones
    Replies:
    0
    Views:
    349
    C Jones
    Jul 10, 2003
  5. Mark Wiewel

    Webservices - POST and GET do not work

    Mark Wiewel, Apr 30, 2004, in forum: ASP .Net Web Services
    Replies:
    1
    Views:
    119
    Mark Wiewel
    Apr 30, 2004
Loading...

Share This Page