Python Cookielib with HTTP Servers that Have Incorrect Date / Timezone Set -
i using python , cookielib
talk http server has date incorrectly set. have no control on server, fixing time not possibility. unfortunately, server's incorrect time messes cookielib
because cookies appear expired.
interestingly, if go same website web browser, browser accepts cookie , gets saved. assume modern webbrowsers come across misconfigured web servers time , see date
header set incorrectly, , adjust cookie expiration dates accordingly.
has come across problem before? there way of handling within python?
i hacked solution includes live-monkey patching of urllib library. not ideal, if others find better way, please let me know:
cook_proc = urllib2.httpcookieprocessor(cookielib.lwpcookiejar()) cookie_processing_lock = threading.lock() def _process_cookies(request, response): '''process cookies, in way can handle servers bad clocks set.''' # real monkey hacking here, put in lock. cookie_processing_lock: # server date. date_header = cookielib.http2time( response.info().getheader('date') or '') # save old cookie parsing function. orig_parse = cookielib.parse_ns_headers # if server date off more hour, we'll adjust it. if date_header: off_by = time.time() - date_header if abs(off_by) > 3600: logging.warning("server off %.1f hrs."%(abs(off_by)/3600)) # create our monkey patched def hacked_parse(ns_headers): try: results = orig_parse(ns_headers) r in results: r_i, (key, val) in enumerate(r): if key == 'expires': r[r_i] = key, val + off_by logging.info("fixing bad cookie " "expiration time for: %s"%r[0][0]) logging.info("cookie results: %s", results) return results except exception e: logging.error("problem parse cookie: %s"%e) raise cookielib.parse_ns_headers = hacked_parse response = cook_proc.http_response(request, response) # make sure set cookie processor back. cookielib.parse_ns_headers = orig_parse
Comments
Post a Comment