本文整理匯總了Python中WMCore.Services.pycurl_manager.RequestHandler.request方法的典型用法代碼示例。如果您正苦於以下問題:Python RequestHandler.request方法的具體用法?Python RequestHandler.request怎麽用?Python RequestHandler.request使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類WMCore.Services.pycurl_manager.RequestHandler
的用法示例。
在下文中一共展示了RequestHandler.request方法的5個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: getDataFromURL
# 需要導入模塊: from WMCore.Services.pycurl_manager import RequestHandler [as 別名]
# 或者: from WMCore.Services.pycurl_manager.RequestHandler import request [as 別名]
def getDataFromURL(url, proxyfilename = None):
"""
Read the content of a URL and return it as a string.
Type of content should not matter, it can be a json file or a tarball for example.
url: the link you would like to retrieve
proxyfilename: the x509 proxy certificate to be used in case auth is required
Returns binary data encoded as a string, which can be later processed
according to what kind of content it represents.
"""
# Get rid of unicode which may cause problems in pycurl
stringUrl = url.encode('ascii')
reqHandler = RequestHandler()
_, data = reqHandler.request(url=stringUrl, params={}, ckey=proxyfilename,
cert=proxyfilename,
capath=HTTPRequests.getCACertPath())
return data
示例2: PyCurlManager
# 需要導入模塊: from WMCore.Services.pycurl_manager import RequestHandler [as 別名]
# 或者: from WMCore.Services.pycurl_manager.RequestHandler import request [as 別名]
class PyCurlManager(unittest.TestCase):
"""Test pycurl_manager module"""
def setUp(self):
"initialization"
self.mgr = RequestHandler()
self.ckey = os.path.join(os.environ['HOME'], '.globus/userkey.pem')
self.cert = os.path.join(os.environ['HOME'], '.globus/usercert.pem')
def testMulti(self):
"""
Test fetch of several urls at once, one of the url relies on CERN SSO.
"""
tfile = tempfile.NamedTemporaryFile()
url1 = "https://cmsweb.cern.ch/dbs/prod/global/DBSReader/help"
url2 = "https://cmsweb.cern.ch/dbs/prod/global/DBSReader/datatiers"
url3 = "https://cms-gwmsmon.cern.ch/prodview/json/site_summary"
cern_sso_cookie(url3, tfile.name, self.cert, self.ckey)
cookie = {url3: tfile.name}
urls = [url1, url2, url3]
data = getdata(urls, self.ckey, self.cert, cookie=cookie)
headers = 0
for row in data:
if '200 OK' in row['headers']:
headers += 1
self.assertTrue(headers, 3)
def testSingle(self):
"""
Test single call to CERN SSO url.
"""
# test RequestHandler
url = "https://cms-gwmsmon.cern.ch/prodview/json/site_summary"
params = {}
tfile = tempfile.NamedTemporaryFile()
cern_sso_cookie(url, tfile.name, self.cert, self.ckey)
cookie = {url: tfile.name}
header, _ = self.mgr.request(url, params, cookie=cookie)
self.assertTrue(header.status, 200)
示例3: Requests
# 需要導入模塊: from WMCore.Services.pycurl_manager import RequestHandler [as 別名]
# 或者: from WMCore.Services.pycurl_manager.RequestHandler import request [as 別名]
class Requests(dict):
"""
Generic class for sending different types of HTTP Request to a given URL
"""
def __init__(self, url='http://localhost', idict=None):
"""
url should really be host - TODO fix that when have sufficient code
coverage and change _getURLOpener if needed
"""
if not idict:
idict = {}
dict.__init__(self, idict)
self.pycurl = idict.get('pycurl', None)
self.capath = idict.get('capath', None)
if self.pycurl:
self.reqmgr = RequestHandler()
# set up defaults
self.setdefault("accept_type", 'text/html')
self.setdefault("content_type", 'application/x-www-form-urlencoded')
self.additionalHeaders = {}
# check for basic auth early, as if found this changes the url
urlComponent = sanitizeURL(url)
if urlComponent['username'] is not None:
self.addBasicAuth( \
urlComponent['username'], urlComponent['password'])
url = urlComponent['url'] # remove user, password from url
self.setdefault("host", url)
# then update with the incoming dict
self.update(idict)
self['endpoint_components'] = urlparse.urlparse(self['host'])
# If cachepath = None disable caching
if 'cachepath' in idict and idict['cachepath'] is None:
self["req_cache_path"] = None
else:
cache_dir = (self.cachePath(idict.get('cachepath'), \
idict.get('service_name')))
self["cachepath"] = cache_dir
self["req_cache_path"] = os.path.join(cache_dir, '.cache')
self.setdefault("timeout", 300)
self.setdefault("logger", logging)
check_server_url(self['host'])
def get(self, uri=None, data={}, incoming_headers={},
encode=True, decode=True, contentType=None):
"""
GET some data
"""
return self.makeRequest(uri, data, 'GET', incoming_headers,
encode, decode, contentType)
def post(self, uri=None, data={}, incoming_headers={},
encode=True, decode=True, contentType=None):
"""
POST some data
"""
return self.makeRequest(uri, data, 'POST', incoming_headers,
encode, decode, contentType)
def put(self, uri=None, data={}, incoming_headers={},
encode=True, decode=True, contentType=None):
"""
PUT some data
"""
return self.makeRequest(uri, data, 'PUT', incoming_headers,
encode, decode, contentType)
def delete(self, uri=None, data={}, incoming_headers={},
encode=True, decode=True, contentType=None):
"""
DELETE some data
"""
return self.makeRequest(uri, data, 'DELETE', incoming_headers,
encode, decode, contentType)
def makeRequest(self, uri=None, data={}, verb='GET', incoming_headers={},
encoder=True, decoder=True, contentType=None):
"""
Wrapper around request helper functions.
"""
if self.pycurl:
result = self.makeRequest_pycurl(uri, data, verb, incoming_headers,
encoder, decoder, contentType)
else:
result = self.makeRequest_httplib(uri, data, verb, incoming_headers,
encoder, decoder, contentType)
return result
def makeRequest_pycurl(self, uri=None, params={}, verb='GET',
incoming_headers={}, encoder=True, decoder=True, contentType=None):
"""
Make HTTP(s) request via pycurl library. Stay complaint with
makeRequest_httplib method.
#.........這裏部分代碼省略.........
示例4: PyCurlManager
# 需要導入模塊: from WMCore.Services.pycurl_manager import RequestHandler [as 別名]
# 或者: from WMCore.Services.pycurl_manager.RequestHandler import request [as 別名]
class PyCurlManager(unittest.TestCase):
"""Test pycurl_manager module"""
def setUp(self):
"initialization"
self.mgr = RequestHandler()
self.ckey = os.path.join(os.environ['HOME'], '.globus/userkey.pem')
self.cert = os.path.join(os.environ['HOME'], '.globus/usercert.pem')
self.cricheader = 'Date: Tue, 06 Nov 2018 14:50:29 GMT\r\nServer: Apache/2.4.6 (CentOS) OpenSSL/1.0.2k-fips mod_wsgi/3.4 Python/2.7.5 mod_gridsite/2.3.4\r\nVary: Cookie\r\nX-Frame-Options: SAMEORIGIN\r\nSet-Cookie: sessionid=bc1xu8zi5rbbsd5fgjuklb2tk2r3f6tw; expires=Sun, 11-Nov-2018 14:50:29 GMT; httponly; Max-Age=432000; Path=/\r\nContent-Length: 32631\r\nContent-Type: application/json\r\n\r\n'
self.dbsheader = 'Date: Tue, 06 Nov 2018 14:39:07 GMT\r\nServer: Apache\r\nCMS-Server-Time: D=1503 t=1541515147806112\r\nTransfer-Encoding: chunked\r\nContent-Type: text/html\r\n\r\n'
self.HTTPheader = 'Date: Tue, 06 Nov 2018 14:50:29 GMT\r\nServer: Apache/2.4.6 (CentOS) OpenSSL/1.0.2k-fips mod_wsgi/3.4 Python/2.7.5 mod_gridsite/2.3.4\r\nVary: Cookie\r\nX-Frame-Options: SAMEORIGIN\r\nSet-Cookie: GRIDHTTP_PASSCODE=2c6da9c96efa2ad0farhda; domain=cms-cric.cern.ch; path=/; secure\r\nContent-Length: 32631\r\nContent-Type: application/json\r\n\r\n'
def testMulti(self):
"""
Test fetch of several urls at once, one of the url relies on CERN SSO.
"""
tfile = tempfile.NamedTemporaryFile()
url1 = "https://cmsweb.cern.ch/dbs/prod/global/DBSReader/help"
url2 = "https://cmsweb.cern.ch/dbs/prod/global/DBSReader/datatiers"
url3 = "https://cms-gwmsmon.cern.ch/prodview/json/site_summary"
cern_sso_cookie(url3, tfile.name, self.cert, self.ckey)
cookie = {url3: tfile.name}
urls = [url1, url2, url3]
data = getdata(urls, self.ckey, self.cert, cookie=cookie)
headers = 0
for row in data:
if '200 OK' in row['headers']:
headers += 1
self.assertTrue(headers, 3)
def testSingle(self):
"""
Test single call to CERN SSO url.
"""
# test RequestHandler
url = "https://cms-gwmsmon.cern.ch/prodview/json/site_summary"
params = {}
tfile = tempfile.NamedTemporaryFile()
cern_sso_cookie(url, tfile.name, self.cert, self.ckey)
cookie = {url: tfile.name}
header, _ = self.mgr.request(url, params, cookie=cookie)
self.assertTrue(header.status, 200)
def testContinue(self):
"""
Test HTTP exit code 100 - Continue
"""
header = "HTTP/1.1 100 Continue\r\n" + self.dbsheader
resp = ResponseHeader(header)
self.assertIsNone(getattr(resp, "status", None))
self.assertEqual(resp.reason, "")
self.assertFalse(resp.fromcache)
self.assertIn("CMS-Server-Time", resp.header)
self.assertIn("Date", resp.header)
self.assertEqual(resp.header['Content-Type'], 'text/html')
self.assertEqual(resp.header['Server'], 'Apache')
self.assertEqual(resp.header['Transfer-Encoding'], 'chunked')
return
def testOK(self):
"""
Test HTTP exit code 200 - OK
"""
header = "HTTP/1.1 200 OK\r\n" + self.dbsheader
resp = ResponseHeader(header)
self.assertEqual(resp.status, 200)
self.assertEqual(resp.reason, "OK")
self.assertFalse(resp.fromcache)
return
def testForbidden(self):
"""
Test HTTP exit code 403 - Forbidden
"""
header = "HTTP/1.1 403 Forbidden\r\n" + self.dbsheader
resp = ResponseHeader(header)
self.assertEqual(resp.status, 403)
self.assertEqual(resp.reason, "Forbidden")
self.assertFalse(resp.fromcache)
return
def testOKCRIC(self):
"""
Test HTTP exit code 200 - OK for a CRIC response header
"""
header = "HTTP/1.1 200 OK\r\n" + self.cricheader
resp = ResponseHeader(header)
self.assertEqual(resp.status, 200)
self.assertEqual(resp.reason, "OK")
self.assertFalse(resp.fromcache)
self.assertIn("Content-Length", resp.header)
self.assertIn("Date", resp.header)
self.assertIn("Server", resp.header)
self.assertIn("sessionid", resp.header['Set-Cookie'])
self.assertEqual(resp.header['Content-Type'], 'application/json')
#.........這裏部分代碼省略.........
示例5: command
# 需要導入模塊: from WMCore.Services.pycurl_manager import RequestHandler [as 別名]
# 或者: from WMCore.Services.pycurl_manager.RequestHandler import request [as 別名]
def command(self, jobs, jobs_lfn, jobs_pfn, jobs_report):
"""
For each job the worker has to complete:
Delete files that have failed previously
Create a temporary copyjob file
Submit the copyjob to the appropriate FTS server
Parse the output of the FTS transfer and return complete and failed files for recording
"""
# Output: {"userProxyPath":"/path/to/proxy","LFNs":["lfn1","lfn2","lfn3"],"PFNs":["pfn1","pfn2","pfn3"],"FTSJobid":'id-of-fts-job', "username": 'username'}
#Loop through all the jobs for the links we have
failure_reasons = []
for link, copyjob in jobs.items():
submission_error = False
status_error = False
fts_job = {}
# Validate copyjob file before doing anything
self.logger.debug("Valid %s" % self.validate_copyjob(copyjob))
if not self.validate_copyjob(copyjob): continue
rest_copyjob = {
"params":{
"bring_online": None,
"verify_checksum": False,
"copy_pin_lifetime": -1,
"max_time_in_queue": self.config.max_h_in_queue,
"job_metadata":{"issuer": "ASO"},
"spacetoken": None,
"source_spacetoken": None,
"fail_nearline": False,
"overwrite": True,
"gridftp": None
},
"files":[]
}
pairs = []
for SrcDest in copyjob:
tempDict = {"sources": [], "metadata": None, "destinations": []}
tempDict["sources"].append(SrcDest.split(" ")[0])
tempDict["destinations"].append(SrcDest.split(" ")[1])
rest_copyjob["files"].append(tempDict)
self.logger.debug("Subbmitting this REST copyjob %s" % rest_copyjob)
url = self.fts_server_for_transfer + '/jobs'
self.logger.debug("Running FTS submission command")
self.logger.debug("FTS server: %s" % self.fts_server_for_transfer)
self.logger.debug("link: %s -> %s" % link)
heade = {"Content-Type ":"application/json"}
buf = StringIO.StringIO()
try:
connection = RequestHandler(config={'timeout': 300, 'connecttimeout' : 300})
except Exception as ex:
msg = str(ex)
msg += str(traceback.format_exc())
self.logger.debug(msg)
try:
response, datares = connection.request(url, rest_copyjob, heade, verb='POST', doseq=True, ckey=self.user_proxy, \
cert=self.user_proxy, capath='/etc/grid-security/certificates', \
cainfo=self.user_proxy, verbose=True)
self.logger.debug("Submission done")
self.logger.debug('Submission header status: %s' % response.status)
self.logger.debug('Submission header reason: %s' % response.reason)
self.logger.debug('Submission result %s' % datares)
except Exception as ex:
msg = "Error submitting to FTS: %s " % url
msg += str(ex)
msg += str(traceback.format_exc())
self.logger.debug(msg)
failure_reasons.append(msg)
submission_error = True
buf.close()
if not submission_error:
res = {}
try:
res = json.loads(datares)
except Exception as ex:
msg = "Couldn't load submission acknowledgment from FTS"
msg += str(ex)
msg += str(traceback.format_exc())
self.logger.debug(msg)
submission_error = True
failure_reasons.append(msg)
if 'job_id' in res:
fileId_list = []
files_res = []
files_ = {}
job_id = res['job_id']
file_url = self.fts_server_for_transfer + '/jobs/' + job_id +'/files'
self.logger.debug("Submitting to %s" % file_url)
file_buf = StringIO.StringIO()
try:
response, files_ = connection.request(file_url, {}, heade, doseq=True, ckey=self.user_proxy, \
cert=self.user_proxy, capath='/etc/grid-security/certificates', \
cainfo=self.user_proxy, verbose=True)
files_res = json.loads(files_)
except Exception as ex:
msg = "Error contacting FTS to retrieve file: %s " % file_url
msg += str(ex)
#.........這裏部分代碼省略.........