views:

289

answers:

1

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  A: 

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?

Forest
I realize the problem, it was working perfectly, but my testpage wasn't.
CMC