Changeset 846
- Timestamp:
- 03/13/07 13:01:22 (1 year ago)
- Files:
-
- pg8000/trunk/pg8000.py (modified) (5 diffs)
- pg8000/trunk/pg8000_test.py (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
pg8000/trunk/pg8000.py
r845 r846 362 362 self.conn = None 363 363 364 def connect(user, host=None, unix_sock=None, port=5432, database=None, password=None, socket_timeout=60 ):364 def connect(user, host=None, unix_sock=None, port=5432, database=None, password=None, socket_timeout=60, ssl=False): 365 365 return DBAPI.ConnectionWrapper(user=user, host=host, 366 366 unix_sock=unix_sock, port=port, database=database, 367 password=password, socket_timeout=socket_timeout )367 password=password, socket_timeout=socket_timeout, ssl=ssl) 368 368 connect = staticmethod(connect) 369 369 … … 692 692 # @keyparam socket_timeout Socket connect timeout measured in seconds. 693 693 # Defaults to 60 seconds. 694 # 695 # @keyparam ssl Use SSL encryption for TCP/IP socket. Defaults to False. 694 696 class Connection(Cursor): 695 def __init__(self, user, host=None, unix_sock=None, port=5432, database=None, password=None, socket_timeout=60 ):697 def __init__(self, user, host=None, unix_sock=None, port=5432, database=None, password=None, socket_timeout=60, ssl=False): 696 698 self._row_desc = None 697 699 try: 698 self.c = Protocol.Connection(unix_sock=unix_sock, host=host, port=port, socket_timeout=socket_timeout )700 self.c = Protocol.Connection(unix_sock=unix_sock, host=host, port=port, socket_timeout=socket_timeout, ssl=ssl) 699 701 #self.c.connect() 700 702 self.c.authenticate(user, password=password, database=database) 701 703 except socket.error, e: 704 print repr(e) 705 print dir(e) 706 print str(e) 707 702 708 raise InterfaceError("communication error", e) 703 709 Cursor.__init__(self, self) … … 729 735 730 736 class Protocol(object): 737 738 class SSLRequest(object): 739 def __init__(self): 740 pass 741 742 def serialize(selF): 743 return struct.pack("!ii", 8, 80877103) 744 731 745 class StartupMessage(object): 732 746 def __init__(self, user, database=None): … … 1070 1084 createFromData = staticmethod(createFromData) 1071 1085 1086 class SSLWrapper(object): 1087 def __init__(self, sslobj): 1088 self.sslobj = sslobj 1089 def send(self, data): 1090 self.sslobj.write(data) 1091 def recv(self, num): 1092 return self.sslobj.read(num) 1093 1072 1094 class Connection(object): 1073 def __init__(self, unix_sock=None, host=None, port=5432, socket_timeout=60 ):1095 def __init__(self, unix_sock=None, host=None, port=5432, socket_timeout=60, ssl=False): 1074 1096 self._client_encoding = "ascii" 1075 1097 if unix_sock == None and host != None: … … 1079 1101 else: 1080 1102 raise ProgrammingError("one of host or unix_sock must be provided") 1081 self._sock.settimeout(socket_timeout)1082 1103 if unix_sock == None and host != None: 1083 1104 self._sock.connect((host, port)) 1084 1105 elif unix_sock != None: 1085 1106 self._sock.connect(unix_sock) 1107 if ssl: 1108 self._send(Protocol.SSLRequest()) 1109 resp = self._sock.recv(1) 1110 if resp == 'S': 1111 self._sock = Protocol.SSLWrapper(socket.ssl(self._sock)) 1112 else: 1113 raise InterfaceError("server refuses SSL") 1114 else: 1115 # settimeout causes ssl failure, on windows. Python bug 1462352. 1116 self._sock.settimeout(socket_timeout) 1086 1117 self._state = "noauth" 1087 1118 self._backend_key_data = None pg8000/trunk/pg8000_test.py
r845 r846 13 13 "database": "pg8000-test", 14 14 "password": "pg8000-test", 15 "socket_timeout": 5 15 "socket_timeout": 5, 16 "ssl": False, 16 17 } 17 18 db = pg8000.Connection(**db_connect)
