def checkPageStatus(url): socket.setdefaulttimeout(TIMEOUT) code = 404; try: a = os.popen("curl -I "+url).readlines() except Exception,e: code = 404
def openUrl(url, timeout = 20): '''A simple way to open url url: the URI to be opened, it must start with 'http://' timeout: time out of connecting to server return: the tuple of status code and error message, HTTP_ERR_RETCODE means failed! ''' errMsg = EMPTY_STR; if None == url: errMsg = PARAM_ERR return (HTTP_ERR_RETCODE, errMsg); if (type(EMPTY_STR) == type(url)) and (EMPTY_STR == url.strip()): errMsg = PARAM_ERR return (HTTP_ERR_RETCODE, errMsg); tempUrl = url; from urllib2 import HTTPError, socket, urlopen; try: socket.setdefaulttimeout(timeout) tempUrl = urlEncode(tempUrl); response = urlopen(tempUrl); if not response: errMsg = "response is none"; return (HTTP_ERR_RETCODE, errMsg); try: response.close(); except Exception, closeE: errMsg = "close response failed," + str(closeE); retCode = 200;
def openUrl(url, timeout = 10): '''A simple way to open url url: the URI to be opened return:the return status code ''' if None == url: return HTTP_ERR_RETCODE; if (type("") == type(url)) and ("" == url.strip()): return HTTP_ERR_RETCODE; try: from urllib2 import socket; socket.setdefaulttimeout(timeout) from urllib2 import urlopen; import inspect; print inspect.getsource(urlopen); response = urlopen(url,timeout=10); if not response: return HTTP_ERR_RETCODE; try: response.close(); except Exception, closeE: print "closeE:" + str(closeE); pass; return response.getcode();
def openUrl(url, timeout=10): '''A simple way to open url url: the URI to be opened return:the return status code ''' if None == url: return HTTP_ERR_RETCODE if (type("") == type(url)) and ("" == url.strip()): return HTTP_ERR_RETCODE try: from urllib2 import socket socket.setdefaulttimeout(timeout) from urllib2 import urlopen import inspect print inspect.getsource(urlopen) response = urlopen(url, timeout=10) if not response: return HTTP_ERR_RETCODE try: response.close() except Exception, closeE: print "closeE:" + str(closeE) pass return response.getcode()
def get_release_version(): version = '' url = "http://www.aifang.com"; socket.setdefaulttimeout(TIMEOUT) try: res = urlopen(url) cinfo = str(res.info()) r = re.compile(r'v=([0-9]+_[0-9]{2,3})') csearch = r.search(cinfo) if csearch != None: version = csearch.group(1) except HTTPError,e: version = ''
def u2open(self, u2request): """ Open a connection. @param u2request: A urllib2 request. @type u2request: urllib2.Requet. @return: The opened file-like urllib2 object. @rtype: fp """ tm = self.options.timeout url = build_opener(HTTPSClientAuthHandler(self.context)) if self.u2ver() < 2.6: socket.setdefaulttimeout(tm) return url.open(u2request) else: return url.open(u2request, timeout=tm)
''' Created on 2012-2-25 @author: fengclient simple wrapper for urllib2 with cookie support(by cookielib) ''' import cookielib import gzip, zlib, struct from StringIO import StringIO from urllib2 import HTTPCookieProcessor, HTTPRedirectHandler, \ HTTPDefaultErrorHandler, build_opener, Request, socket #from http_parser.pyparser import HttpParser socket.setdefaulttimeout(10) class MyHTTPRedirectHandler(HTTPRedirectHandler): def http_error_302(self, req, fp, code, msg, headers): return fp http_error_301 = http_error_303 = http_error_307 = http_error_302 def urlopen(url, data=None, cookiejar=None): ''' get url content and store cookies. rfc2964 default cookie policy with domain strict is applied. (http_message,cookiejar,resp_data) is returned. resp_data is decoded if gzip/deflate is present. ''' policy = cookielib.DefaultCookiePolicy(rfc2965=True, strict_ns_domain=cookielib.DefaultCookiePolicy.DomainStrict) cj = cookielib.CookieJar(policy) cookiehandler = HTTPCookieProcessor(cj) redirect_handler = MyHTTPRedirectHandler()