why i cant login this site using python in a post method
Question
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.
OTHER TIPS
Try using a password manager:
from here:
# create a password manager
password_mgr = urllib2.HTTPPasswordMgrWithDefaultRealm()
# Add the username and password.
# If we knew the realm, we could use it instead of ``None``.
top_level_url = "http://example.com/foo/"
password_mgr.add_password(None, top_level_url, username, password)
handler = urllib2.HTTPBasicAuthHandler(password_mgr)
# create "opener" (OpenerDirector instance)
opener = urllib2.build_opener(handler)
# use the opener to fetch a URL
opener.open(a_url)
# Install the opener.
# Now all calls to urllib2.urlopen use our opener.
urllib2.install_opener(opener)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow