pythonメモ

某所にアクセスする際にhttplib.HTTPConnectionを用いる場合

        host='mixi.jp'
        path='/login.pl'
        if host not in self.connections:
            postdata = {}
            postdata['email'] = user
            postdata['password'] = passwd
            postdata['next_url'] = '/list_friend.pl'
            import urllib
            params = urllib.urlencode(postdata)
            headers = {
                "Content-type": "application/x-www-form-urlencoded",
                'Connection': 'keep-alive',
                "Accept": "Text/plain"
                }
            conn = httplib.HTTPConnection(host)
            conn.request("POST",path,params,headers)
            r = conn.getresponse()
            r.read()
            c = r.getheader('Set-Cookie').split(',')
            import re
            c = [re.sub(';.*','',f) for f in c]
            path = r.getheader('Location')
            headers = {
                "Content-type": "application/x-www-form-urlencoded",
                'Connection': 'keep-alive',
                'Cookie': c[0] +'; '+ c[1],
                "Accept": "text/plain"
                }
            conn.request("POST",path, params, headers)

本当にメモ。これを関数でくくればアクセスできると思います。