Python: urllib/urllib2/httplib confusion

后端 未结 8 1849
长情又很酷
长情又很酷 2020-12-04 07:01

I\'m trying to test the functionality of a web app by scripting a login sequence in Python, but I\'m having some troubles.

Here\'s what I need to do:

  1. D
相关标签:
8条回答
  • 2020-12-04 07:33

    I had to do this exact thing myself recently. I only needed classes from the standard library. Here's an excerpt from my code:

    from urllib import urlencode
    from urllib2 import urlopen, Request
    
    # encode my POST parameters for the login page
    login_qs = urlencode( [("username",USERNAME), ("password",PASSWORD)] )
    
    # extract my session id by loading a page from the site
    set_cookie = urlopen(URL_BASE).headers.getheader("Set-Cookie")
    sess_id = set_cookie[set_cookie.index("=")+1:set_cookie.index(";")]
    
    # construct headers dictionary using the session id
    headers = {"Cookie": "session_id="+sess_id}
    
    # perform login and make sure it worked
    if "Announcements:" not in urlopen(Request(URL_BASE+"login",headers=headers), login_qs).read():
        print "Didn't log in properly"
        exit(1)
    
    # here's the function I used after this for loading pages
    def download(page=""):
        return urlopen(Request(URL_BASE+page, headers=headers)).read()
    
    # for example:
    print download(URL_BASE + "config")
    
    0 讨论(0)
  • 2020-12-04 07:36

    Besides the fact that you may be missing a cookie, there might be some field(s) in the form that you are not POSTing to the webserver. The best way would be to capture the actual POST from a web browser. You can use LiveHTTPHeaders or WireShark to snoop the traffic and mimic the same behaviour in your script.

    0 讨论(0)
提交回复
热议问题