简体   繁体   中英

HTTP requests on localhost via python urllib and urllib2 are very slow

I have written a simple class using urllib and urllib2 to send http requests and get the response. However, any request to localhost using its IP Address is very slow.

IP Address of LOCALHOST is = 192.168.158.27

import urllib2,urllib,re,datetime,time

class HTTPRequest():
    def __init__(self,**kargs):
        self._response = None
        self._buffer = None
        self._conn = urllib2.build_opener(urllib2.HTTPCookieProcessor())
        urllib2.install_opener(self._conn)

    def _encode_url(self,**kargs):
        try:
            params = urllib.urlencode(kargs)
        except:
            raise HTTPError("Failed to encode URL parameters..")

        return str(params)

    def _request(self,url=None,params=None):
        try:
            self._buffer = self._conn.open(url,params)
            self._response = self._buffer.read()
        except ValueError:
             raise HTTPError("Invalid URL %s" % url)
        except:
            raise HTTPError("Failed to send HTTP(s) Request")
        return str(self._response)

class HTTPError(Exception):
    pass


PARAM_PASSWORD = 'password'
PARAM_USER = 'userName'
PARAM_ACTION = 'a'
PARAM_RID = 'rid'
PARAM_XO = 'xo'
PARAM_START_TIME = 't1'
PARAM_END_TIME = 't2'
PARAM_PATH = 'path'

BOOLEAN_TRUE = 'true'
BOOLEAN_FALSE = 'false'

ACTION_SIGNIN = 'signIn'
ACTION_SEARCH = 'search'
ACTION_GET_NEXT_RESULTS = 'getNextResults'

STATUS_SUCCEEDED = 'succeeded'

DEFAULT_WAIT = 5

host = "192.168.158.27"
user = "admin"
password = "admin"
protocol = "https"
port = 8443
query = "vm[=name rx (?i) *]&[@cpuUsage rx b .+][@cpuUsagemhz rx b .+]"
start_time = "10/05/2013 16:16:00"
end_time = "10/05/2013 17:16:00"
base_url = "%s://%s:%d" % (protocol,host,port)
login_url = "%s/user" % base_url

http = HTTPRequest()

attributes = {PARAM_PASSWORD : password,
              PARAM_USER : user,
              PARAM_ACTION : ACTION_SIGNIN,
              PARAM_RID : 1000,
              PARAM_XO : BOOLEAN_TRUE}

params = http._encode_url(**attributes)

if not http._request(login_url,params):
        print "Login Failed.."

else:
        print "Login Successful.. \n"



rid = 1000

search_url = "%s/Search" % base_url

status = STATUS_SUCCEEDED
hasMoreData = BOOLEAN_TRUE
completed = BOOLEAN_FALSE
total = 0
processed = 1
responseContent = ""
xml_dict = {}
_response = ""

attributes = {PARAM_START_TIME : start_time,
              PARAM_END_TIME : end_time,
              PARAM_ACTION : ACTION_SEARCH,
              PARAM_RID : rid,
              PARAM_PATH : query}

print "URL PARAMETERS :"
print "\tBase url = %s" % base_url

for param in attributes:
    print "\t%s = %s" % (param,attributes[param])

#Query Execution Start Time
start = datetime.datetime.now()

while True:
    params = http._encode_url(**attributes)
    if hasMoreData == BOOLEAN_TRUE:
        #Delay 10ms
        time.sleep(10/1000)

        #Send HTTP Request
        response = http._request(search_url,params)

        pattern = re.match(".*?hasMoreData=\"(.*?)\".*?",response)
        if pattern:
            hasMoreData = pattern.group(1)

        pattern = re.match(".*?status=\"(.*?)\".*?",response)
        if pattern:
            status = pattern.group(1)

        pattern = re.match(".*?completed=\"(.*?)\".*?",response)
        if pattern:
            completed = pattern.group(1)

        pattern = re.match(".*?processed=\"(.*?)\".*?",response)
        if pattern:
            processed = pattern.group(1)

        pattern = re.match(".*?total=\"(.*?)\".*?",response)
        if pattern:
            total = pattern.group(1)

        pattern = re.match(".*?matched=\"(.*?)\".*?",response)
        if pattern:
            matched = pattern.group(1)

        attributes = {PARAM_ACTION : ACTION_GET_NEXT_RESULTS,
                      PARAM_RID : rid}

        if matched != "0":
            response = re.sub(r'\n',"",response)
            matchObj = re.search(r'(<Resource.*</Resource>)',response)

            resp_match = ""
            if matchObj:
                resp_match = matchObj.group(1)

            responseContent = str(responseContent) + str(resp_match)
    else:
        #Query Execution Completed
        #Query Execution End Time
        end = datetime.datetime.now()

        print "RESULTS : "
        print "\tStatus = %s"%status
        print "\tHas More Data = %s"%hasMoreData
        print "\tCompleted = %s"%completed
        print "\tProcessed = %s"%processed
        print "\tTotal = %s"%total
        print "\tMatched = %s"%matched
        print "\nQuery Execution Started : %s" % start
        print "Query Execution Ended   : %s\n" % end

        if total != processed:
            err = "The number records processed did not match"
            err += " with the number of records completed."
            print err

        if not status == STATUS_SUCCEEDED:
            err = "The responce status is not 'succeeded'"
            print err

        if completed == BOOLEAN_FALSE:
            err = "The repsponse is completed. "
            err += "However, the flag is set to 'False'"
            print err
        break

尝试使用127.0.0.1代替本地IP。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM