[Zope-CVS] CVS: Packages/WebService - Transports.py:1.2 Utility.py:1.2 WSDLTools.py:1.2 XMLSchema.py:1.3
Brian Lloyd
brian@digicool.com
Thu, 29 Nov 2001 16:40:49 -0500
Update of /cvs-repository/Packages/WebService
In directory cvs.zope.org:/tmp/cvs-serv32741
Modified Files:
Transports.py Utility.py WSDLTools.py XMLSchema.py
Log Message:
Added a custom urlopen() implementation to Transports that implements
socket timeout. That makes it possible to avoid blocking forever on
non-messaging related network tasks (like retrieving WSDL or XMLSchema
documents from URLs).
=== Packages/WebService/Transports.py 1.1 => 1.2 ===
# FOR A PARTICULAR PURPOSE.
-import string, httplib, smtplib, socket
+import string, httplib, smtplib, urllib, socket
from TimeoutSocket import TimeoutSocket
from TimeoutSocket import TimeoutError
+from StringIO import StringIO
from urlparse import urlparse
@@ -191,7 +192,6 @@
def connect(self):
self.sock = TimeoutSocket(self.timeout)
- #self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.sock.connect((self.host, self.port))
@@ -202,8 +202,7 @@
self.timeout = timeout
def connect(self):
-# sock = TimeoutSocket(self.timeout)
- sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
+ sock = TimeoutSocket(self.timeout)
sock.connect((self.host, self.port))
realsock = getattr(sock, '_sock', sock)
ssl = socket.ssl(realsock, self.key_file, self.cert_file)
@@ -236,3 +235,59 @@
(code,msg)=self.getreply()
if self.debuglevel >0 : print "connect:", msg
return (code,msg)
+
+
+def urlopen(url, timeout=20, redirects=None):
+ """A minimal urlopen replacement hack that supports timeouts for http.
+ Note that this supports GET only."""
+ scheme, host, path, params, query, frag = urlparse(url)
+ if not scheme in ('http', 'https'):
+ return urllib.urlopen(url)
+ if params: path = '%s;%s' % (path, params)
+ if query: path = '%s?%s' % (path, query)
+ if frag: path = '%s#%s' % (path, frag)
+
+ if scheme == 'https':
+ if not hasattr(socket, 'ssl'):
+ raise ValueError(
+ 'This Python installation does not have SSL support.'
+ )
+ conn = TimeoutHTTPS(host, None, timeout)
+ else:
+ conn = TimeoutHTTP(host, None, timeout)
+
+ conn.putrequest('GET', path)
+ conn.putheader('Connection', 'close')
+ conn.endheaders()
+ response = None
+ while 1:
+ response = conn.getresponse()
+ if response.status != 100:
+ break
+ conn._HTTPConnection__state = httplib._CS_REQ_SENT
+ conn._HTTPConnection__response = None
+
+ status = response.status
+
+ # If we get an HTTP redirect, we will follow it automatically.
+ if status >= 300 and status < 400:
+ location = response.msg.getheader('location')
+ if location is not None:
+ response.close()
+ if redirects is not None and redirects.has_key(location):
+ raise RecursionError(
+ 'Circular HTTP redirection detected.'
+ )
+ if redirects is None:
+ redirects = {}
+ redirects[location] = 1
+ return urlopen(location, timeout, redirects)
+ raise HTTPResponse(response)
+
+ if not (status >= 200 and status < 300):
+ raise HTTPResponse(response)
+
+ body = StringIO(response.read())
+ response.close()
+ return body
+
=== Packages/WebService/Utility.py 1.1 => 1.2 ===
from UserDict import UserDict
from StringIO import StringIO
-from urllib import urlopen
+from Transports import urlopen
import xml.dom.minidom
import weakref
=== Packages/WebService/WSDLTools.py 1.1 => 1.2 ===
# FOR A PARTICULAR PURPOSE.
-from urllib import urlopen, basejoin
from Utility import DOM, Collection
from XMLSchema import XMLSchema
from XMLWriter import XMLWriter
+from Transports import urlopen
from StringIO import StringIO
+from urllib import basejoin
import md5
=== Packages/WebService/XMLSchema.py 1.2 => 1.3 ===
# FOR A PARTICULAR PURPOSE.
+import string, types, base64, re
from Utility import DOM, Collection
-from urllib import urlopen, basejoin
+from Transports import urlopen
from StringIO import StringIO
-import string, types, base64, re
+from urllib import basejoin
class SchemaReader: