source: ndgCommon/trunk/ndg/common/src/clients/http/generichttpclient.py @ 5443

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/ndgCommon/trunk/ndg/common/src/clients/http/generichttpclient.py@5443
Revision 5443, 2.3 KB checked in by cbyrom, 12 years ago (diff)

Add keyword to allow headers info to be set in generichttpclient.

Line 
1'''
2 Simple client to get resources over http
3 
4 @author: B Lawrence?, C Byrom, Tessella, Feb 09
5'''
6from xml.etree import ElementTree as ET
7import urllib2, logging, socket
8from ndg.common.src.lib.utilities import httpify
9           
10class GenericHTTPClient(object):
11    '''
12    Provides a generic HTTP request class
13    '''
14    def __init__(self, proxyServer=None):
15        '''
16        Constructor for client
17        @keyword proxyServer: a string representing a proxy server url to use
18        when opening an http request
19        '''
20        logging.debug("Instantiating GenericHTTPClient")
21        if proxyServer:
22            logging.debug("- using proxy server, '%s'" %proxyServer)
23            proxy = httpify(proxyServer)
24            proxyHandler = urllib2.ProxyHandler({'http':proxy})
25        else:
26            proxyHandler = urllib2.ProxyHandler({})
27       
28        self.opener = urllib2.build_opener(proxyHandler)
29        logging.debug("GenericHTTPClient instantiated")
30
31       
32    def readURL(self, url, payload = None, headersDict = None):
33        '''
34        Retrieve contents of url - or raise IOError if a problem is found
35        @param url: url to open and retrive data from
36        @keyword payload: data to POST in the request
37        @keyword headersDict: dict with headers info to use in the request
38        @raise IOError if problem experienced whilst opening url
39        @return contents of url
40        '''
41        logging.info("Getting data from url: %s" %url)
42        url = httpify(url)
43        inputs = {}
44        if payload:
45            inputs['data'] = payload
46           
47        if headersDict:
48            inputs['headers'] = headersDict
49           
50        request=urllib2.Request(url, **inputs)
51        response='Cannot obtain remote file: '
52        try:
53            # NB, if the connection is invalid, it will fail at this point
54            f = self.opener.open(request)
55            return f.read()
56       
57        except urllib2.URLError,e:
58            if hasattr(e,'reason'):
59                response += 'No access to server [%s]'%e.reason
60            else:
61                response += str(e)
62        except socket.error:
63            response += 'Network Socket problem'
64        except Exception,e:
65            response += '[%s]'%str(e)
66       
67        logging.error(response)
68        raise IOError(response) 
Note: See TracBrowser for help on using the repository browser.