Logging in and using cookies in pycurl

前端 未结 3 1030
隐瞒了意图╮
隐瞒了意图╮ 2021-02-04 15:45

I need to download a file that is on a password protected page. To get to the page manually I first have to authenticate via an ordinary login page. I want to use curl to fetc

3条回答
  •  庸人自扰
    2021-02-04 16:03

    You should store cookie first and then read from it:

    C.setopt(pycurl.COOKIEJAR, 'cookie.txt')
    C.setopt(pycurl.COOKIEFILE, 'cookie.txt')
    

    Here what curl --help returned:

    -b, --cookie STRING/FILE  String or file to read cookies from (H)
    -c, --cookie-jar FILE  Write cookies to this file after operation (H)
    

    See this sample:

    def connect(self):
        '''
        Connect to NGNMS server
        '''
        host_url = self.ngnms_host + '/login'
    
        c = pycurl.Curl()
        c.setopt(c.URL, host_url)
        c.setopt(pycurl.TIMEOUT, 10)
    
        c.setopt(pycurl.FOLLOWLOCATION, 1)
        c.setopt(pycurl.POSTFIELDS, 'j_username={ngnms_user}&j_password={ngnms_password}'.format(**self.ngnms_login))
        c.setopt(pycurl.COOKIEJAR, 'data/ngnms.cookie')
    
        # c.setopt(c.VERBOSE, True)
    
        c.setopt(pycurl.SSL_VERIFYPEER, 0);
        session = c
        return session
    

提交回复
热议问题