Author: remi
Date: 2009-02-17 16:05:57 +0100 (Tue, 17 Feb 2009)
New Revision: 3717
Modified:
software_suite_v2/tuxware/pytuxisalive/trunk/src/tuxisalive/api/TuxHTTPRequest.py
Log:
* HTTP request module can now be directly connected to the HTTP server when it
are in the same process. The effect is the avoiding of the communications
through the HTTP TCP/IP protocol.
Modified:
software_suite_v2/tuxware/pytuxisalive/trunk/src/tuxisalive/api/TuxHTTPRequest.py
===================================================================
---
software_suite_v2/tuxware/pytuxisalive/trunk/src/tuxisalive/api/TuxHTTPRequest.py
2009-02-17 15:01:58 UTC (rev 3716)
+++
software_suite_v2/tuxware/pytuxisalive/trunk/src/tuxisalive/api/TuxHTTPRequest.py
2009-02-17 15:05:57 UTC (rev 3717)
@@ -18,6 +18,11 @@
import httplib
import time
+try:
+ from tuxisalive.lib.httpserver.TDBaseRequestHandler import
TDBaseRequestHandler
+except:
+ pass
+
class TuxHTTPRequest(object):
"""TuxHTTPRequest is a sender of request to a Tuxdroid service server.
The resulting xml data is automatically parsed and returned to a
@@ -32,6 +37,11 @@
"""
self.__hostPort = "%s:%d" % (host, port)
self.__mutex = threading.Lock()
+ if host == 'INTERNAL':
+ print "INTERNAL mode"
+ self.__directHandler = True
+ else:
+ self.__directHandler = False
def request(self, cmd, method = "GET"):
"""Make a request to the server.
@@ -48,63 +58,79 @@
}
# Try to connect to the server
self.__mutex.acquire()
- h = httplib.HTTP(self.__hostPort)
- try:
- h.connect()
- except:
- self.__mutex.release()
- return xmlStruct
- # Create the request
- h.putrequest(method, cmd)
- h.putheader("Accept", "text/html")
- h.putheader("Accept", "text/xml")
- h.putheader("Accept-Charset", "iso-8859-1,*,utf-8")
- h.endheaders()
- # Send the request (try 5 times)
- s = False
- c = 0
- contentLength = 0
- while True:
+ if not self.__directHandler:
+ h = httplib.HTTP(self.__hostPort)
try:
- errcode, errmsg, headers = h.getreply()
- contentLength = int(headers['Content-Length'])
- s = True
- break
+ h.connect()
except:
- pass
- c += 1
- if c > 5:
- break
- time.sleep(0.05)
-
- if not s:
- self.__mutex.release()
- return xmlStruct
-
- if contentLength > 0:
- # Get the result stream
- f = h.getfile()
- try:
- resultStr = f.read()
- f.close()
- except:
self.__mutex.release()
return xmlStruct
- # Attempt to encoding the xml stream to utf-8
+ # Create the request
+ h.putrequest(method, cmd)
+ h.putheader("Accept", "text/html")
+ h.putheader("Accept", "text/xml")
+ h.putheader("Accept-Charset", "iso-8859-1,*,utf-8")
+ h.endheaders()
+ # Send the request (try 5 times)
+ s = False
+ c = 0
+ contentLength = 0
+ while True:
+ try:
+ errcode, errmsg, headers = h.getreply()
+ contentLength = int(headers['Content-Length'])
+ s = True
+ break
+ except:
+ pass
+ c += 1
+ if c > 5:
+ break
+ time.sleep(0.05)
+
+ if not s:
+ self.__mutex.release()
+ return xmlStruct
+
+ if contentLength > 0:
+ # Get the result stream
+ f = h.getfile()
+ try:
+ resultStr = f.read()
+ f.close()
+ except:
+ self.__mutex.release()
+ return xmlStruct
+ # Attempt to encoding the xml stream to utf-8
+ try:
+ u = resultStr.decode("latin-1")
+ resultStr = u.encode("utf-8")
+ except:
+ pass
+ # Parse the xml
+ xmlStruct = self.__parseXml(resultStr)
+ else:
+ xmlStruct['server_run'] = 'Success'
+
try:
- u = resultStr.decode("latin-1")
- resultStr = u.encode("utf-8")
+ h.disconnect()
except:
pass
- # Parse the xml
- xmlStruct = self.__parseXml(resultStr)
else:
- xmlStruct['server_run'] = 'Success'
-
- try:
- h.disconnect()
- except:
- pass
+ baseHandler = TDBaseRequestHandler()
+ headers, content = baseHandler.getHeaderAndContent(cmd)
+ if content != None:
+ # Attempt to encoding the xml stream to utf-8
+ try:
+ u = content.decode("latin-1")
+ content = u.encode("utf-8")
+ except:
+ pass
+ # Parse the xml
+ xmlStruct = self.__parseXml(content)
+ else:
+ xmlStruct['server_run'] = 'Success'
+
self.__mutex.release()
return xmlStruct
------------------------------------------------------------------------------
Open Source Business Conference (OSBC), March 24-25, 2009, San Francisco, CA
-OSBC tackles the biggest issue in open source: Open Sourcing the Enterprise
-Strategies to boost innovation and cut costs with open source participation
-Receive a $600 discount off the registration fee with the source code: SFAD
http://p.sf.net/sfu/XcvMzF8H
_______________________________________________
Tux-droid-svn mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/tux-droid-svn