Created
October 12, 2012 23:52
-
-
Save SecurityForUs/3882375 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def head(self, options): | |
try: | |
if options['method'] is not "": | |
url = "%s/%s" % (options['url'], options['method']) | |
else: | |
url = "%s" % (options['url']) | |
except: | |
raise APIException("No API method call provided.") | |
try: | |
print "!! url: %s !!" % (url) | |
if self.proxy['https']: | |
req = requests.head(url, headers=options['headers'], data=options['data'], verify = True, prefetch = False, proxies = self.proxy) | |
else: | |
req = requests.head(url, headers=options['headers'], data=options['data'], verify = True, prefetch = False) | |
except KeyError, key: | |
raise APIException("Invalid or empty option passed to backup server: %s" % (key)) | |
except requests.exceptions.SSLError: | |
raise APIException("SSL connection error. Unable to verify SSL certificate.") | |
return {'status' : req.status_code, 'hdl' : req} | |
""" | |
Checks to see if data exists on SL's servers, checking for 0 byte range to reduce latency and | |
overhead as much as possible. | |
""" | |
def checkexists(self, obj = "", date = None): | |
# So we don't buffer a whole lot of data, we only request the first byte of the file | |
if obj is not "": | |
self.options['headers'].update({'Range' : "bytes=0-0"}) | |
# Set the method to be the object we want to check against | |
self.options['method'] = "%s" % (obj) | |
# If the date is specified, it overrides the default (current) date set by the API server | |
if date is not None: | |
self.options['url'] = "%s/%s" % (self.options['url'], date) | |
# Attempt to get our data | |
try: | |
#if date is not None and obj is "": | |
req = self.head(self.options) | |
#else: | |
# req = self.get(self.options) | |
print req['hdl'].status_code | |
except APIException, e: | |
self.reset_headers() | |
# Uh-oh, sphagehtti ohs! We should never reach this point for this call | |
self.log.critical("Unable to verify %s exists: %s" % (obj, e)) | |
self.reset_headers() | |
""" | |
- 200 : File exists on server | |
- 204 : Folder exists on server | |
- 206 : Meta object (file) exists on server | |
""" | |
if req['status'] in [200,204,206]: | |
return 1 | |
return 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment