Cookies with urllib

自古美人都是妖i 提交于 2019-12-13 22:41:31

问题


This will probably seem like a really simple question, and I am quite confused as to why this is so difficult for me.

I would like to write a function that takes three inputs: [url, data, cookies] that will use urllib (not urllib2) to get the contents of the requested url. I figured it'd be simple, so I wrote the following:

def fetch(url, data = None, cookies = None):
  if isinstance(data, dict): data = urllib.urlencode(data)
  if isinstance(cookies, dict):
    # TODO: find a better way to do this
    cookies = "; ".join([str(key) + "=" + str(cookies[key]) for key in cookies])
  opener = urllib.FancyURLopener()
  opener.addheader("Cookie", cookies)
  obj = opener.open(url, data)
  result = obj.read()
  obj.close()
  return result

This doesn't work, as far as I can tell (can anyone confirm that?) and I'm stumped.


回答1:


You didn't say what went wrong when you tried it, or what http server you're testing with. Did the request complete? Did the server fail to recognize your cookies? One thing that jumps out at me is that you're potentially joining multiple cookies into a single header field. Does it work if you use separate Cookie: header fields?



来源:https://stackoverflow.com/questions/2886573/cookies-with-urllib

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!