Python http 模块,cookies() 实例源码

我们从Python开源项目中,提取了以下6个代码示例,用于说明如何使用http.cookies()

项目:directory-ui-buyer    作者:uktrade    | 项目源码 | 文件源码
def test_unsubscribe_api_failure(
    mock_unsubscribe, api_response_400, logged_in_client
):
    logged_in_client.cookies = SimpleCookie(
        {settings.SSO_PROXY_SESSION_COOKIE: 1}
    )
    mock_unsubscribe.return_value = api_response_400

    response = logged_in_client.post(reverse('unsubscribe'))

    mock_unsubscribe.assert_called_once_with(sso_session_id='213')
    view = views.EmailUnsubscribeView
    assert response.status_code == http.client.OK
    assert response.template_name == view.failure_template
项目:packaging    作者:blockstack    | 项目源码 | 文件源码
def test_other_http_imports(self):
        import http
        import http.server
        import http.cookies
        import http.cookiejar
        self.assertTrue(True)
项目:packaging    作者:blockstack    | 项目源码 | 文件源码
def test_future_moves(self):
        """
        Ensure everything is available from the future.moves interface that we
        claim and expect. (Issue #104).
        """
        from future.moves.collections import Counter, OrderedDict   # backported to Py2.6
        from future.moves.collections import UserDict, UserList, UserString

        from future.moves import configparser
        from future.moves import copyreg

        from future.moves.itertools import filterfalse, zip_longest

        from future.moves import html
        import future.moves.html.entities
        import future.moves.html.parser

        from future.moves import http
        import future.moves.http.client
        import future.moves.http.cookies
        import future.moves.http.cookiejar
        import future.moves.http.server

        from future.moves import queue

        from future.moves import socketserver

        from future.moves.subprocess import check_output              # even on Py2.6
        from future.moves.subprocess import getoutput, getstatusoutput

        from future.moves.sys import intern

        from future.moves import urllib
        import future.moves.urllib.error
        import future.moves.urllib.parse
        import future.moves.urllib.request
        import future.moves.urllib.response
        import future.moves.urllib.robotparser

        try:
            # Is _winreg available on Py2? If so, ensure future.moves._winreg is available too:
            import _winreg
        except ImportError:
            pass
        else:
            from future.moves import winreg

        from future.moves import xmlrpc
        import future.moves.xmlrpc.client
        import future.moves.xmlrpc.server

        from future.moves import _dummy_thread
        from future.moves import _markupbase
        from future.moves import _thread
项目:taobaoscrapy    作者:hunterhug    | 项目源码 | 文件源码
def getHtml(url, postdata={}):
    """
    ???????cookie
    ?????????????POST???

    """
    # COOKIE??????
    filename = 'cookie.txt'

    # ????MozillaCookieJar??????????
    cj = http.cookiejar.MozillaCookieJar(filename)
    # cj =http.cookiejar.LWPCookieJar(filename)

    # ??????cookie?????
    # ignore_discard??????cookies???????????
    # ignore_expires??????????? cookies????????????
    # ??????????COOKIE
    if os.path.exists(filename):
        cj.load(filename, ignore_discard=True, ignore_expires=True)
    # ????COOKIE
    if os.path.exists('../subcookie.txt'):
        cookie = open('../subcookie.txt', 'r').read()
    else:
        cookie = 'ddd'
    # ????COOKIE????????
    opener = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(cj))

    # ???????
    opener.addheaders = [('User-Agent',
                          'Mozilla/5.0 (iPad; U; CPU OS 4_3_3 like Mac OS X; en-us) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8J2 Safari/6533.18.5'),
                         ('Referer',
                          'http://s.m.taobao.com'),
                         ('Host', 'h5.m.taobao.com'),
                         ('Cookie', cookie)]

    # ????
    urllib.request.install_opener(opener)
    # ?????POST
    if postdata:
        # ??URL??
        postdata = urllib.parse.urlencode(postdata)

        # ????
        html_bytes = urllib.request.urlopen(url, postdata.encode()).read()
    else:
        html_bytes = urllib.request.urlopen(url).read()

    # ??COOKIE????
    cj.save(ignore_discard=True, ignore_expires=True)
    return html_bytes


# ?????????? (Windows)
项目:taobaoscrapy    作者:hunterhug    | 项目源码 | 文件源码
def getHtml(url, daili='', postdata={}):
    """
    ???????cookie
    ?????????????POST???

    """
    # COOKIE??????
    filename = 'cookie.txt'

    # ????MozillaCookieJar??????????
    cj = http.cookiejar.MozillaCookieJar(filename)
    # cj =http.cookiejar.LWPCookieJar(filename)

    # ??????cookie?????
    # ignore_discard??????cookies???????????
    # ignore_expires??????????? cookies????????????
    # ??????????COOKIE
    if os.path.exists(filename):
        cj.load(filename, ignore_discard=True, ignore_expires=True)
    # ????COOKIE
    if os.path.exists('../subcookie.txt'):
        cookie = open('../subcookie.txt', 'r').read()
    else:
        cookie = 'ddd'
    # ????COOKIE????????
    proxy_support = urllib.request.ProxyHandler({'http': 'http://' + daili})
    # ??????
    if daili:
        print('??:' + daili + '??')
        opener = urllib.request.build_opener(proxy_support, urllib.request.HTTPCookieProcessor(cj),
                                             urllib.request.HTTPHandler)
    else:
        opener = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(cj))

    # ???????
    opener.addheaders = [('User-Agent',
                          'Mozilla/5.0 (iPad; U; CPU OS 4_3_3 like Mac OS X; en-us) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8J2 Safari/6533.18.5'),
                         ('Referer',
                          'http://s.m.taobao.com'),
                         ('Host', 'h5.m.taobao.com'),
                         ('Cookie', cookie)]

    # ????
    urllib.request.install_opener(opener)
    # ?????POST
    if postdata:
        # ??URL??
        postdata = urllib.parse.urlencode(postdata)

        # ????
        html_bytes = urllib.request.urlopen(url, postdata.encode()).read()
    else:
        html_bytes = urllib.request.urlopen(url).read()

    # ??COOKIE????
    cj.save(ignore_discard=True, ignore_expires=True)
    return html_bytes


# ?????????? (Windows)
项目:python_tool    作者:hunterhug    | 项目源码 | 文件源码
def getHtml(url, daili='', postdata={}, header=[]):
    """
    ???????cookie
    ?????????????POST???
    """
    # COOKIE??????
    filename = 'cookie.txt'

    # ????MozillaCookieJar??????????
    cj = http.cookiejar.MozillaCookieJar(filename)
    # cj =http.cookiejar.LWPCookieJar(filename)

    # ??????cookie?????
    # ignore_discard??????cookies???????????
    # ignore_expires??????????? cookies????????????
    # ??????????COOKIE
    if os.path.exists(filename):
        cj.load(filename, ignore_discard=True, ignore_expires=True)
    # ????COOKIE
    if os.path.exists('../subcookie.txt'):
        cookie = open('../subcookie.txt', 'r').read()
    else:
        cookie = ''
    # ????COOKIE????????
    proxy_support = urllib.request.ProxyHandler({'http': 'http://' + daili})
    # ??????
    if daili:
        print('??:' + daili + '??')
        opener = urllib.request.build_opener(proxy_support, urllib.request.HTTPCookieProcessor(cj),
                                             urllib.request.HTTPHandler)
    else:
        opener = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(cj))
    if header:
        pass
    else:
        header = [('User-Agent',
                   'Mozilla/5.0 (iPad; U; CPU OS 4_3_3 like Mac OS X; en-us) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8J2 Safari/6533.18.5'),
                  ('Referer', 'http://s.m.taobao.com'),
                  ('Host', 'h5.m.taobao.com'),
                  ('Cookie', cookie)]
    # ???????
    opener.addheaders = header

    # ????
    urllib.request.install_opener(opener)
    # ?????POST
    if postdata:
        # ??URL??
        postdata = urllib.parse.urlencode(postdata)

        # ????
        html_bytes = urllib.request.urlopen(url, postdata.encode()).read()
    else:
        html_bytes = urllib.request.urlopen(url).read()

    # ??COOKIE????
    cj.save(ignore_discard=True, ignore_expires=True)
    return html_bytes