pythonauthenticationurllib2cookielib

why i cant login this site using python in a post method


this is my code to login a site using python :

import urllib2, cookielib
cookie_support= urllib2.HTTPCookieProcessor(cookielib.CookieJar())
opener = urllib2.build_opener(cookie_support, urllib2.HTTPHandler)
urllib2.install_opener(opener)
content = urllib2.urlopen('http://192.168.1.200/order/index.php?op=Login&ac=login&userName=%E8%B5%B5%E6%B1%9F%E6%98%8E&userPwd=123').read()

print content

it show :

{"title":"login error","body":"username or password error","data":{"status":1}}

but the username and password is right , i can login this site using firefox ,

so what can i do ,

thanks


Solution

  • You are making a GET request. To make a POST request, use:

    content = urllib2.urlopen(
        'http://192.168.1.200/order/index.php",
        'op=Login&ac=login&userName=%E8%B5%B5%E6%B1%9F%E6%98%8E&userPwd=123').read()
    

    The urlopen method sends a POST request if data (second argument) is passed to it.