def request(url, headers={}, output="content"): util.debug('request: %s' % url) req = urllib2.Request(url, headers=headers) req.add_header('User-Agent', util.UA) if util._cookie_jar is not None: util._cookie_jar.add_cookie_header(req) data = '' try: response = urllib2.urlopen(req) while True: try: tmp = response.read() except http.client.IncompleteRead as icread: data = data + icread.partial #.decode('utf-8') continue else: data = data + tmp #.decode('utf-8') break code = response.code info = response.info() response.close() except urllib2.HTTPError, error: code = error.code data = util._solve_http_errors(url, error) info = None
def post(url, data, headers={}, output="content"): postdata = urllib.urlencode(data) req = urllib2.Request(url, postdata, headers) req.add_header('User-Agent', util.UA) if util._cookie_jar is not None: util._cookie_jar.add_cookie_header(req) try: response = urllib2.urlopen(req) data = response.read() code = response.code response.close() except urllib2.HTTPError, error: data = util._solve_http_errors(url, error) code = error.code
def request(url, headers={}, output="content"): util.debug('request: %s' % url) req = urllib2.Request(url, headers=headers) req.add_header('User-Agent', util.UA) if util._cookie_jar is not None: util._cookie_jar.add_cookie_header(req) try: response = urllib2.urlopen(req) data = response.read() code = response.code response.close() except urllib2.HTTPError, error: code = error.code data = util._solve_http_errors(url, error)
def post_json(url, data, headers={}, output="content"): postdata = json.dumps(data) headers['Content-Type'] = 'application/json' req = urllib2.Request(url, postdata, headers) req.add_header('User-Agent', util.UA) if util._cookie_jar is not None: util._cookie_jar.add_cookie_header(req) try: response = urllib2.urlopen(req) data = response.read() code = response.code response.close() except urllib2.HTTPError, error: data = util._solve_http_errors(url, error) code = error.code
def request(url, headers={}, output="content", method=None): util.debug('request: %s' % url) req = urllib2.Request(url, headers=headers) req.add_header('User-Agent', util.UA) if util._cookie_jar is not None: util._cookie_jar.add_cookie_header(req) data = '' if method: req.get_method = lambda: method try: response = urllib2.urlopen(req) while True: try: tmp = response.read() except http.client.IncompleteRead as icread: data = data + icread.partial # .decode('utf-8') continue else: data = data + tmp # .decode('utf-8') break code = response.code info = response.info() response.close() except urllib2.HTTPError as error: # odchytava iba HTTP chyby, nie chyby spojenia! code = error.code data = util._solve_http_errors(url, error) info = None except (urllib2.URLError, Exception) as e: URLError(e) util.debug('len(data) %s' % len(data)) if output == "content": try: return data.decode('utf-8') except: return data if (output == "info"): return (data, code, info) else: return (data, code)
def post_json(url, data, headers={}, output="content"): postdata = json.dumps(data) headers['Content-Type'] = 'application/json' req = urllib2.Request(url, postdata, headers) req.add_header('User-Agent', util.UA) if util._cookie_jar is not None: util._cookie_jar.add_cookie_header(req) try: response = urllib2.urlopen(req) data = response.read() code = response.code response.close() except urllib2.HTTPError as error: data = util._solve_http_errors(url, error) code = error.code except (urllib2.URLError, Exception) as e: URLError(e) if output == "content": return data else: return (data, code)
def post(url, data, headers={}, output="content"): postdata = urllib.urlencode(data) req = urllib2.Request(url, postdata, headers) req.add_header('User-Agent', util.UA) util.debug("[SC] post: %s" % url) if util._cookie_jar is not None: util._cookie_jar.add_cookie_header(req) try: response = urllib2.urlopen(req, timeout=120) data = response.read() code = response.code response.close() except urllib2.HTTPError as error: data = util._solve_http_errors(url, error) code = error.code except (urllib2.URLError, Exception) as e: URLError(e) if (output == "content"): return data else: return (data, code)