Python: urllib/urllib2/httplib confusion

后端 未结 8 1848
长情又很酷
长情又很酷 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:11

    Focus on urllib2 for this, it works quite well. Don't mess with httplib, it's not the top-level API.

    What you're noting is that urllib2 doesn't follow the redirect.

    You need to fold in an instance of HTTPRedirectHandler that will catch and follow the redirects.

    Further, you may want to subclass the default HTTPRedirectHandler to capture information that you'll then check as part of your unit testing.

    cookie_handler= urllib2.HTTPCookieProcessor( self.cookies )
    redirect_handler= HTTPRedirectHandler()
    opener = urllib2.build_opener(redirect_handler,cookie_handler)
    

    You can then use this opener object to POST and GET, handling redirects and cookies properly.

    You may want to add your own subclass of HTTPHandler to capture and log various error codes, also.

    0 讨论(0)
  • 2020-12-04 07:12

    Try twill - a simple language that allows users to browse the Web from a command-line interface. With twill, you can navigate through Web sites that use forms, cookies, and most standard Web features. More to the point, twill is written in Python and has a python API, e.g:

    from twill import get_browser
    b = get_browser()
    
    b.go("http://www.python.org/")
    b.showforms()
    
    0 讨论(0)
  • 2020-12-04 07:16

    @S.Lott, thank you. Your suggestion worked for me, with some modification. Here's how I did it.

    data = urllib.urlencode(params)
    url = host+page
    request = urllib2.Request(url, data, headers)
    response = urllib2.urlopen(request)
    
    cookies = CookieJar()
    cookies.extract_cookies(response,request)
    
    cookie_handler= urllib2.HTTPCookieProcessor( cookies )
    redirect_handler= HTTPRedirectHandler()
    opener = urllib2.build_opener(redirect_handler,cookie_handler)
    
    response = opener.open(request)
    
    0 讨论(0)
  • 2020-12-04 07:23

    Here's my take on this issue.

    #!/usr/bin/env python
    
    import urllib
    import urllib2
    
    
    class HttpBot:
        """an HttpBot represents one browser session, with cookies."""
        def __init__(self):
            cookie_handler= urllib2.HTTPCookieProcessor()
            redirect_handler= urllib2.HTTPRedirectHandler()
            self._opener = urllib2.build_opener(redirect_handler, cookie_handler)
    
        def GET(self, url):
            return self._opener.open(url).read()
    
        def POST(self, url, parameters):
            return self._opener.open(url, urllib.urlencode(parameters)).read()
    
    
    if __name__ == "__main__":
        bot = HttpBot()
        ignored_html = bot.POST('https://example.com/authenticator', {'passwd':'foo'})
        print bot.GET('https://example.com/interesting/content')
        ignored_html = bot.POST('https://example.com/deauthenticator',{})
    
    0 讨论(0)
  • 2020-12-04 07:23

    Funkload is a great web app testing tool also. It wraps webunit to handle the browser emulation, then gives you both functional and load testing features on top.

    0 讨论(0)
  • 2020-12-04 07:28

    I'd give Mechanize (http://wwwsearch.sourceforge.net/mechanize/) a shot. It may well handle your cookie/headers transparently.

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