def dump_qrcode(ticket, path): try: file, rsp = HTTP.download( HTTP.joinurl(WX_URL_GET_QRCODE, ticket=ticket), path) except urllib.error.HTTPError as err: logger.warning('get qrcode: %s from weixin failed: %s', ticket, err) return None else: return file
def __init__(self, ssh_transport, host='', port=None, strict=None, localport=DEFAULT_LOCAL_PORT_START): self.localport = localport self.ssh_transport = ssh_transport HTTP.__init__(self, host, port, strict)
def get_file_size(url): types = urllib.splittype(url) host, res = urllib.splithost(types[1]) h = HTTP() h.connect(host) h.putrequest('HEAD', res) h.putheader('Host', host) h.endheaders() status, reason, headers = h.getreply() return float(headers['Content-Length'])
def identify(host): h = HTTP(host) h.putrequest('GET', 'http://%s/' % host) h.putheader('Accept', '*/*') h.endheaders() errcode, errmsg, response = h.getreply() h.close() patterns.attempts = response.headers try: str = response['server'] except: str = None return (str, None)
def action_delete(): vip = sys.argv[3] h = HTTP('home.shaunkruger.com:8087') h.putrequest('DELETE','/module/mod_cluster_admin/vip/'+vip) h.putheader('Authorization','Basic '+base64.standard_b64encode('skruger:testing')) h.endheaders() errcode, errmsg, headers = h.getreply() if errcode == 200: f = h.getfile() print f.read() return
def gethttpfile(url, size=1024*1024): from urllib import splithost from httplib import HTTP if not url.startswith('http:'): raise ValueError, "URL %s" % url host, selector = splithost(url[5:]) h = HTTP(host) h.putrequest('GET', url) h.endheaders() h.getreply() res = h.getfile().read(size) h.close() return res
def checkURL(url): p = urlparse(url) h = HTTP(p[1]) h.putrequest('HEAD', p[2]) h.endheaders() print h.getreply() if h.getreply()[0] == 200: return 1 else: return 0
def check_url(url): p = urlparse(url) h = HTTP(p[1]) h.putrequest('HEAD', p[2]) h.endheaders() print h.getreply()[0] if h.getreply()[0] == 200: return True else: return False
def create_menu(menu): logger.info(json.dumps(menu)) rsp = HTTP.post(WX_URL_CREATE_MENU, json.dumps(menu, ensure_ascii=False), access_token=get_access_token()) return json.loads(rsp)
def get_web_auth_access_token(code): return json.loads( HTTP.get(WX_URL_WEB_AUTH_ACCESS_TOKEN, appid=APP_ID, secret=APP_SECRET, code=code, grant_type='authorization_code'))
def get_redirect_url(url): types = urllib.splittype(url) host, res = urllib.splithost(types[1]) h = HTTP() h.connect(host) h.putrequest('HEAD', res) h.endheaders() status, reason, headers = h.getreply() return headers['location']
def fetch_id_from_Google(self, cid, lac, country): latitude = 0 longitude = 0 device = "Motorola C123" country = Translator.Country[country] b_string = pack( ">hqh2sh13sh5sh3sBiiihiiiiii", 21, 0, len(country), country, len(device), device, len("1.3.1"), "1.3.1", len("Web"), "Web", 27, 0, 0, 3, 0, cid, lac, 0, 0, 0, 0, ) http = HTTP("www.google.com", 80) http.putrequest("POST", "/glm/mmap") http.putheader("Content-Type", "application/binary") http.putheader("Content-Length", str(len(b_string))) http.endheaders() http.send(b_string) code, msg, headers = http.getreply() try: bytes = http.file.read() (a, b, errorCode, latitude, longitude, c, d, e) = unpack(">hBiiiiih", bytes) latitude /= 1000000.0 longitude /= 1000000.0 status = CellIDDBStatus.CONFIRMED except: status = CellIDDBStatus.NOT_IN_DB return status, latitude, longitude
def checkLink(self, url): parsedURL = urlparse(url) httpConn = HTTP(parsedURL[1]) httpConn.putrequest('HEAD',parsedURL[2]) httpConn.endheaders() if httpConn.getreply()[0] == 200: return 1 else: return 0
def checkURL(url): p = urlparse(url) h = HTTP(p[1]) h.putrequest('HEAD', p[2]) h.endheaders() if h.getreply()[0] == 200: return 1 else: return 0
def checkUrlExists(url): import httplib from httplib import HTTP from urlparse import urlparse log.debug("Checking if '" + url + "' is a URL") try: p = urlparse(url) h = HTTP(p[1]) h.putrequest('HEAD', p[2]) h.endheaders() log.info("String '" + url + "' is a URL") # return h.getreply()[0] == httplib.OK return True except Exception: log.debug("String '" + url + "' isn't a URL") return False
def checkURL(url): p = urlparse(url) h = HTTP(p[1]) h.putrequest('HEAD', p[2]) h.endheaders() reply = h.getreply() print "reply:", reply print url, "response code:", reply[0]
def __makeRequest(self, url, path): from httplib import HTTP http = HTTP(HTTP_PROXY or url.replace("http://", "")) http.putrequest("GET", url + path) http.putheader("Accept", "text/plain") http.endheaders() htcode, errmsg, headers = http.getreply() if htcode not in (200, 403, 404): raise HTTPCaptachaUnexpectedResponse(htcode, errmsg) #endif file = http.getfile() content = file.read() file.close() return htcode, content
def URL_exists(url): p = urlparse(url) h = HTTP(p[1]) h.putrequest('HEAD', p[2]) h.endheaders() if h.getreply()[0] == 200: return True else: return False
def urlExists(url): parsed_url = urlparse(url) http = HTTP(parsed_url[1]) http.putrequest('HEAD', parsed_url[2]) http.endheaders() reply = http.getreply() if reply[0] == 200: return True return False
def get_access_token(): if _access_token[ 'token'] is None or _access_token['timestamp'] + 7000 <= now_sec(): _access_token['token'] = json.loads( HTTP.get(WX_URL_GET_ACCESS_TOKEN, grant_type='client_credential', appid=APP_ID, secret=APP_SECRET))['access_token'] _access_token['timestamp'] = now_sec() logger.info('access_token is: %s', _access_token['token']) return _access_token['token']
def main(): cur_url = "http://zh.wikipedia.org/wiki/Category:%E9%A0%81%E9%9D%A2%E5%88%86%E9%A1%9E" #"http://zh.wikipedia.org/wiki/Wikipedia:%E5%88%86%E9%A1%9E%E7%B4%A2%E5%BC%95" cur_alias = "分类" cts = {} its = {} # ItemName(Wiki Item URL) : ([Alias1, Alias2, Alias3, ...], [InItemName1, InItemName, ..], [OutItemName1, OutItemName2, ..]) h = HTTP() h.connect("zh.wikipedia.org") qcs = Queue.Queue() qns = Queue.Queue() qcs.put({"url": cur_url, "alias": cur_alias}) #tp = ThreadPool(10) tds = [Thread(target=worker, args=(qcs if i % 2 else qns, qcs, qns, cts, its)) for i in xrange(10)] t_start = time.time() for i in tds: i.start() time.sleep(5) for i in tds: i.join() #worker_c(qcs, qns, cts, its) #rqs = makeRequests(worker_c, args) #[tp.putRequest(req) for req in rqs] #try: # tp.joinAllDismissedWorkers() #except KeyboardInterrupt: # tp.joinAllDismissedWorkers() with open("CTS.db", "wb") as f: pickle.dump(cts, f) with open("ITS.db", "wb") as f: pickle.dump(its, f) print "CTS:", cts print "___________________________" print "ITEMS:", its print "Coust Time: %s" % (time.time() - t_start)
def action_list(): h = HTTP(surrogate_conf.get('hostname')) h.putrequest('GET','/module/mod_cluster_admin/vip') userpass = "******" % (surrogate_conf.get('username'),surrogate_conf.get('password')) h.putheader('Authorization','Basic '+base64.standard_b64encode(userpass)) h.endheaders() errcode, errmsg, headers = h.getreply() if errcode == 200: f = h.getfile() data= f.read() vip = json.loads(data) # Convert json string to python array object formatstr = "%-35s %-8s %s" # reusable format string for header and data output lines print formatstr % ("Address", "Status","Nodes") print "===============================================================================" for v in vip["items"]: print formatstr % (v["address"], v["status"],v["nodes"]) elif errcode == 401: print "Authentication error." else: print "HTTP %s: %s" % (errcode,errmsg) return
def URL_exists(url): verbose = True ''' Checks that a URL exists (answer != 404) and returns the size. Returns None if does not exists, the size in bytes otherwise. ''' if verbose: sys.stderr.write('Checking %s ' % url) p = urlparse(url) h = HTTP(p[1]) h.putrequest('HEAD', p[2]) h.endheaders() code, status, message = h.getreply() #@UnusedVariable if verbose: sys.stderr.write('\t%d\n' % code) if code == 404: return None else: return int(message['content-length'])
def getStatusCode(self, url): """ Status code """ try: p = urlparse(url) h = HTTP(p[1]) h.putrequest('HEAD', p[2]) h.endheaders() return h.getreply()[0] except Exception: return 110
def urlcheck(url): u = urlparse(url) h = HTTP(u.geturl()) print u.geturl() h.putrequest('HEAD',u.geturl()) h.endheaders() r = h.getreply() return isvalid(r)
def __makeRequest(self, url, path): from httplib import HTTP http = HTTP(HTTP_PROXY or url.replace("http://", "")) http.putrequest("GET", url + path) http.putheader("Accept", "text/plain") http.endheaders() htcode, errmsg, headers = http.getreply() if htcode not in (200, 403, 404): raise HTTPCaptachaUnexpectedResponse(htcode, errmsg) # endif file = http.getfile() content = file.read() file.close() return htcode, content
def check_url(url): try: p = urlparse(url) h = HTTP(p[1]) h.putrequest('HEAD', p[2]) h.endheaders() if h.getreply()[0] == 200: return True else: return False except Exception, e: log(str(e)) return False
def checkURL(self, url): """Try to connect to a given url. Result is True if url returns HTTP code 200, in any other case (like unreachable server or wrong HTTP code) result will be False """ try: p = urlparse(url) h = HTTP(p[1]) h.putrequest('HEAD', p[2]) h.endheaders() if h.getreply()[0] == 200: return True else: return False except Exception, e: return False
def get_unlimit_qrcode_ticket(arg): args = { 'action_name': 'QR_LIMIT_SCENE', 'action_info': { 'scene': { 'scene_id': arg } } } rsp = HTTP.post(WX_URL_CREATE_QRCODE, json.dumps(args), access_token=get_access_token()) logger.info('get a unlimit qrcode, arg: %s, ret: %s', arg, rsp) rsp = json.loads(rsp) return rsp['ticket']
def check_url(url): """check_url(url:str) -> bool Check if a URL exists using the url's header. """ try: p = urlparse(url) h = HTTP(p[1]) h.putrequest('HEAD', p[2]) h.endheaders() if h.getreply()[0] == 200: return True else: return False except: return False
def _checkURL(url): """ Check if a url is alive (returns code 200(OK) or 401 (unauthorized)) """ try: p = urlparse(url) h = HTTP(p[1]) h.putrequest('HEAD', p[2]) h.endheaders() r = h.getreply() if r[0] == 200 or r[ 0] == 401: # CloudMan UI is pwd protected so include 401 return True except Exception: # No response or no good response pass return False
def get_jsapi_sign(url): if _jsapi_ticket['ticket'] is None or _jsapi_ticket[ 'timestamp'] + 7000 <= now_sec(): access_token = get_access_token() _jsapi_ticket['ticket'] = json.loads( HTTP.get(WX_URL_GET_JSAPI_TICKET, access_token=access_token, type='jsapi'))['ticket'] _jsapi_ticket['timestamp'] = now_sec() noncestr = randstr() timestamp = now_sec() sign = _build_jsapi_sign(_jsapi_ticket['ticket'], noncestr, timestamp, url) return { 'noncestr': noncestr, 'sign': sign, 'timestamp': timestamp, 'appid': APP_ID }
def _checkURL(url): """ Check if a url is alive (returns code 200(OK) or 401 (unauthorized)) """ try: p = urlparse(url) h = HTTP(p[1]) h.putrequest('HEAD', p[2]) h.endheaders() r = h.getreply() if r[0] == 200 or r[0] == 401: # CloudMan UI is pwd protected so include 401 return True except Exception: # No response or no good response pass return False
def check_url(site_id): con = pymongo.Connection(['10.0.0.8:27017','10.0.0.8:27018','10.0.0.8:27019'])#Base.site_collection url = con.find_one({"id":site_id})['reference'] path = urlparse(url) header = HTTP(path[1]) try: header.putrequest('HEAD', path[2]) header.endheaders() except: con.update({"id":site_id},{"$set":{"avaible":False}}) return 0 else: con.update({"id":site_id},{"$set":{"avaible":True}}) con.update({"id":site_id},{"$set":{"last_checked":datetime.datetime(datetime.date.today().year,datetime.date.today().month,datetime.date.today().day)}}) if header.getreply()[0] == 200: con.update({"id":site_id},{"$set":{"avaible":True}});return 1 else: con.update({"id":site_id},{"$set":{"avaible":False}}) return 0
def download_web_page(domain, url): try: h = HTTP(domain) h.putrequest("GET", url) h.putheader('Accept', 'text/html') h.putheader('Accept', 'text/plain') h.endheaders() except: return(None) print "Placed request, downloading web page..." try: errcode, errmsg, headers = h.getreply() except: sys.stderr.write("Error in receiving response from " + domain + \ '\n') return None if errcode != 200: sys.stderr.write("Error in receiving response from " + domain + \ '\n') return None results = h.getfile().read() return(results)
def __init__(self, host='', port=None, **ssl): HTTP.__init__(self, host, port) try: self.ssl_ctx = ssl['ssl_context'] except KeyError: self.ssl_ctx = SSL.Context('sslv23')
def __call__(self,*args,**kw): method=self.method if method=='PUT' and len(args)==1 and not kw: query=[args[0]] args=() else: query=[] for i in range(len(args)): try: k=self.args[i] if kw.has_key(k): raise TypeError, 'Keyword arg redefined' kw[k]=args[i] except IndexError: raise TypeError, 'Too many arguments' headers={} for k, v in self.headers.items(): headers[translate(k,dashtrans)]=v method=self.method if headers.has_key('Content-Type'): content_type=headers['Content-Type'] if content_type=='multipart/form-data': return self._mp_call(kw) else: content_type=None if not method or method=='POST': for v in kw.values(): if hasattr(v,'read'): return self._mp_call(kw) can_marshal=type2marshal.has_key for k,v in kw.items(): t=type(v) if can_marshal(t): q=type2marshal[t](k,v) else: q='%s=%s' % (k,quote(v)) query.append(q) url=self.rurl if query: query='&'.join(query) method=method or 'POST' if method == 'PUT': headers['Content-Length']=str(len(query)) if method != 'POST': url="%s?%s" % (url,query) query='' elif not content_type: headers['Content-Type']='application/x-www-form-urlencoded' headers['Content-Length']=str(len(query)) else: method=method or 'GET' if (self.username and self.password and not headers.has_key('Authorization')): headers['Authorization']=( "Basic %s" % encodestring('%s:%s' % (self.username,self.password)).replace( '\012','') ) try: h=HTTP(self.host, self.port) h.putrequest(method, self.rurl) for hn,hv in headers.items(): h.putheader(translate(hn,dashtrans),hv) h.endheaders() if query: h.send(query) ec,em,headers=h.getreply() response =h.getfile().read() except: raise NotAvailable, RemoteException( NotAvailable,sys.exc_info()[1],self.url,query) if (ec - (ec % 100)) == 200: return (headers,response) self.handleError(query, ec, em, headers, response)
#a = "000E00000000000000000000000000001B0000000000000000000000030000" #b = hex(cid)[2:].zfill(8) + hex(lac)[2:].zfill(8) #c = hex(divmod(mnc,100)[1])[2:].zfill(8) + hex(divmod(mnc,100)[0])[2:].zfill(8) #string = (a + b + c + "FFFFFFFF00000000").decode("hex") #r = urllib.urlopen("http://www.google.com/glm/mmap",string).read().encode("hex") #if len(r) > 14: # lon, lat = float(int(r[14:22], 16)) / 1000000, float(int(r[22:30], 16)) / 1000000 cid, lac, mnc = [int(cid, 16), int(lac, 16), int(mnc)] country = 'fr' device = 'Nokia N95 8Gb' b_string = pack('>hqh2sh13sh5sh3sBiiihiiiiii', 21, 0, len(country), country, len(device), device, len('1.3.1'), "1.3.1", len('Web'), "Web", 27, 0, 0, 3, 0, cid, lac, 0, 0, 0, 0) http = HTTP('www.google.com', 80) http.putrequest('POST', '/glm/mmap') http.putheader('Content-Type', 'application/binary') http.putheader('Content-Length', str(len(b_string))) http.endheaders() http.send(b_string) code, msg, headers = http.getreply() bytes = http.file.read() (a, b, errorCode, latitude, longitude, c, d, e) = unpack(">hBiiiiih", bytes) lat = latitude / 1000000.0 lon = longitude / 1000000.0 print "[+] Coordinates : %f:%f" % (lat, lon) if not BTS.if_already_mapped(lat, lon):
def make_connection(self, host): self.realhost = host h = HTTP(self.proxy) return h
from urllib import splithost from httplib import HTTP if os.path.exists("/dev/shm"): TMPDIR = "/dev/shm" else: TMPDIR = "/tmp" TMPDIR += "/hda-analyzer" print "Using temporary directory: %s" % TMPDIR print "You may remove this directory when finished or if you like to" print "download the most recent copy of hda-analyzer tool." if not os.path.exists(TMPDIR): os.mkdir(TMPDIR) for f in FILES: dest = TMPDIR + '/' + f if os.path.exists(dest): print "File cached " + dest continue print "Downloading file %s" % f host, selector = splithost(URL[5:]) h = HTTP(host) h.putrequest('GET', URL + f) h.endheaders() h.getreply() contents = h.getfile().read(2 * 1024 * 1024) h.close() open(dest, "w+").write(contents) print "Downloaded all files, executing %s" % FILES[0] os.system("python2 %s" % TMPDIR + '/' + FILES[0] + ' ' + ' '.join(sys.argv[1:]))
def __init__(self, ssh_transport, host="", port=None, strict=None, localport=DEFAULT_LOCAL_PORT_START): self.localport = localport self.ssh_transport = ssh_transport HTTP.__init__(self, host, port, strict)
def __init__(self, host='', port=0, timeout=10.0): self.connect_timeout = timeout HTTP.__init__(self, host, port)
def fetch_latlong_http(self, query): http = HTTP('www.google.com', 80) http.putrequest('POST', '/glm/mmap') http.putheader('Content-Type', 'application/binary') http.putheader('Content-Length', str(len(query))) http.endheaders() http.send(query) code, msg, headers = http.getreply() result = http.file.read() return result
def doLookup(cellId, lac, host="www.google.com", port=80): from string import replace from struct import unpack page = "/glm/mmap" http = HTTP(host, port) result = None errorCode = 0 content_type, body = encode_request(cellId, lac) http.putrequest('POST', page) http.putheader('Content-Type', content_type) http.putheader('Content-Length', str(len(body))) http.endheaders() http.send(body) errcode, errmsg, headers = http.getreply() result = http.file.read() # be nice to the web service, will not pause on memoized coordinates time.sleep(0.75) # could need some modification to get the answer: here I just need # to get the 5 first characters if (errcode == 200): (a, b, errorCode, latitude, longitude, c, d, e) = unpack(">hBiiiiih",result) latitude = latitude / 1000000.0 longitude = longitude / 1000000.0 return latitude, longitude
def _setup(self, conn): HTTP._setup(self, conn) conn.set_ssh_transport(self.ssh_transport) conn.set_local_port(self.localport)
headers = [('Host', 'www.comcentral.com'), ('Accept', 'application/x-dvi, application/postscript, video/*, video/mpeg, image/*, audio/*, application/applefile, application/x-metamail-patch, sun-deskset-message, mail-file, default, postscript-file, audio-file, x-sun-attachment, text/enriched, text/richtext, application/andrew-inset, x-be2, application/postscript, message/external-body, message/partial, image/x-xwd, image/gif, image/*, image/jpeg, audio/basic, audio/*, text/html, text/plain, application/x-wais-source, application/html, video/mpeg, image/jpeg, image/x-tiff, image/x-rgb, image/x-png, image/x-xbm, image/gif, application/postscript, */*;q=0.001'), ('Accept-Encoding', 'gzip, compress'), ('Accept-Language', 'en'), ('Pragma', 'no-cache'), ('Cache-Control', 'no-cache'), ('User-Agent', 'Lynx/2.6 libwww-FM/2.14'), ('From', '"a-fan" <*****@*****.**>'), ('Referer', 'http://www.comcentral.com/mst/mstpoll.htm'), ('Content-type', 'application/x-www-form-urlencoded'), ('Content-length', '8')] text='vote=' + str(episode) n = n - 1 h = HTTP('www.comcentral.com') h.debuglevel = 1 h.putrequest('POST', '/cgi-bin/rbox/pollboy.pl') for (hn, hv) in headers: if (hn == 'From'): hv = 'user' + str(n) + '@blah.com' h.putheader(hn, hv) #print "hn:", hn, "hv:", hv h.endheaders() h.send(text) errcode, errmsg, headers = h.getreply() if errcode == 200: f = h.getfile()
def __call__(self, *args, **kw): method = self.method if method == 'PUT' and len(args) == 1 and not kw: query = [args[0]] args = () else: query = [] for i in range(len(args)): try: k = self.args[i] if kw.has_key(k): raise TypeError, 'Keyword arg redefined' kw[k] = args[i] except IndexError: raise TypeError, 'Too many arguments' headers = {} for k, v in self.headers.items(): headers[translate(k, dashtrans)] = v method = self.method if headers.has_key('Content-Type'): content_type = headers['Content-Type'] if content_type == 'multipart/form-data': return self._mp_call(kw) else: content_type = None if not method or method == 'POST': for v in kw.values(): if hasattr(v, 'read'): return self._mp_call(kw) can_marshal = type2marshal.has_key for k, v in kw.items(): t = type(v) if can_marshal(t): q = type2marshal[t](k, v) else: q = '%s=%s' % (k, quote(v)) query.append(q) url = self.rurl if query: query = '&'.join(query) method = method or 'POST' if method == 'PUT': headers['Content-Length'] = str(len(query)) if method != 'POST': url = "%s?%s" % (url, query) query = '' elif not content_type: headers['Content-Type'] = 'application/x-www-form-urlencoded' headers['Content-Length'] = str(len(query)) else: method = method or 'GET' if (self.username and self.password and not headers.has_key('Authorization')): headers['Authorization'] = ("Basic %s" % encodestring( '%s:%s' % (self.username, self.password)).replace('\012', '')) try: h = HTTP(self.host, self.port) h.putrequest(method, self.rurl) for hn, hv in headers.items(): h.putheader(translate(hn, dashtrans), hv) h.endheaders() if query: h.send(query) ec, em, headers = h.getreply() response = h.getfile().read() except: raise NotAvailable, RemoteException(NotAvailable, sys.exc_info()[1], self.url, query) if (ec - (ec % 100)) == 200: return (headers, response) self.handleError(query, ec, em, headers, response)
from httplib import HTTP req = HTTP("www.zoo-berlin.de") req.putrequest("GET", "/?Knut") req.putheader("Accept", "text/html") req.putheader("User-Agent", "Python26") req.endheaders() ec, em, h = req.getreply() print ec, em fd = req.getfile() textlines = fd.read() print textlines fd.close()
def post_multipart(host, port, selector, fields, files): content_type, body = encode_multipart_formdata(fields, files) h = HTTPConnection(host, port) h.putrequest('POST', selector) h.putheader('content-type', content_type) h.putheader('content-length', str(len(body))) h.endheaders() if _python2: h.send(body) else: h.send(body.encode('utf-8')) if _python2: errcode, errmsg, headers = h.getreply() if errcode != 200: raise HTTPException("%s: %s" % (errcode, errmsg)) return h.file.read() else: res = h.getresponse() if res.status != 200: raise HTTPException("%s: %s" % (res.status, res.reason)) return res.read()
from httplib import HTTP req = HTTP("www.example.com") req.putrequest("GET", "/index.html") req.putheader("Accept", "text/html") req.putheader("User-Agent", "MyPythonScript") req.endheaders() ec, em, h = req.getreply() print(ec, em) # 200 OK fd = req.getfile() textlines = fd.read() fd.close()
def RetrieveAsFile(self, host, path=''): from httplib import HTTP try: h = HTTP(host) except: self.logprint("Failed to create HTTP connection to %s... is the network available?" % (host)) return None h.putrequest('GET',path) h.putheader('Accept','text/html') h.putheader('Accept','text/plain') h.endheaders() errcode, errmsg, headers = h.getreply() if errcode != 200: self.logprint("HTTP error code %d: %s" % (errcode, errmsg)) return None f = h.getfile() return f
def send_request(self, request, response): h = HTTP(urlparse(request.uri)[1]) #TODO: split out port, userinfo h.putrequest(request.method, request.uri) for field_name, field_value in request.representation.headers.items(): if field_name == "Content-Length": continue h.putheader(field_name, field_value.string) if request.representation.has_body: h.putheader('Content-Length', str(len(request.representation.body))) #FIXME: hmm. accesses body. options? h.putheader('User-Agent', self.user_agent) # FIXME: use header dict, don't override h.endheaders() if request.representation.has_body: h.send(request.representation.body) #FIXME: use iterator? status_code, status_phrase, headers = h.getreply() response_type = status.lookup.get(status_code, None) if response_type is not None: response.__class__ = response_type response.status_code = status_code response.status_phrase = status_phrase response.representation.headers.parseMessage(headers) #FIXME: split entity and message hdrs response.representation._body_iter = h.getfile() #FIXME: iterator, shouldn't have to do this _ ... if not isinstance(response, status.Successful): raise response
def RetrieveAsFile(self, host, path=''): from httplib import HTTP try: h = HTTP(host) except: self.logprint( "Failed to create HTTP connection to %s... is the network available?" % (host)) return None h.putrequest('GET', path) h.putheader('Accept', 'text/html') h.putheader('Accept', 'text/plain') h.endheaders() errcode, errmsg, headers = h.getreply() if errcode != 200: self.logprint("HTTP error code %d: %s" % (errcode, errmsg)) return None f = h.getfile() return f