2828from __future__ import division
2929
3030from collections import deque
31+ import re
3132
3233from .bolt import connect , Response , RUN , PULL_ALL
3334from .compat import integer , string , urlparse
34- from .constants import DEFAULT_PORT , ENCRYPTED_DEFAULT , TRUST_DEFAULT , TRUST_SIGNED_CERTIFICATES
35+ from .constants import DEFAULT_PORT , ENCRYPTION_DEFAULT , TRUST_DEFAULT , TRUST_SIGNED_CERTIFICATES , ENCRYPTION_ON , \
36+ ENCRYPTION_NON_LOCAL
3537from .exceptions import CypherError , ProtocolError , ResultError
3638from .ssl_compat import SSL_AVAILABLE , SSLContext , PROTOCOL_SSLv23 , OP_NO_SSLv2 , CERT_REQUIRED
3739from .summary import ResultSummary
4042
4143DEFAULT_MAX_POOL_SIZE = 50
4244
45+ localhost = re .compile (r"^(localhost|127(\.\d+){3})$" , re .IGNORECASE )
46+
4347
4448class AuthToken (object ):
4549 """ Container for auth information
@@ -70,7 +74,40 @@ def driver(url, **config):
7074
7175
7276class Driver (object ):
73- """ Accessor for a specific graph database resource.
77+ """ A :class:`.Driver` is an accessor for a specific graph database
78+ resource. It provides both a template for sessions and a container
79+ for the session pool. All configuration and authentication settings
80+ are collected by the `Driver` constructor; should different settings
81+ be required, a new `Driver` instance should be created.
82+
83+ :param address: address of the remote server as either a `bolt` URI
84+ or a `host:port` string
85+ :param config: configuration and authentication details (valid keys are listed below)
86+
87+ `auth`
88+ An authentication token for the server, for example
89+ ``basic_auth("neo4j", "password")``.
90+
91+ `der_encoded_server_certificate`
92+ The server certificate in DER format, if required.
93+
94+ `encrypted`
95+ Encryption level: one of :attr:`.ENCRYPTION_ON`, :attr:`.ENCRYPTION_OFF`
96+ or :attr:`.ENCRYPTION_NON_LOCAL`. The default setting varies
97+ depending on whether SSL is available or not. If it is,
98+ :attr:`.ENCRYPTION_NON_LOCAL` is the default.
99+
100+ `max_pool_size`
101+ The maximum number of sessions to keep idle in the session
102+ pool.
103+
104+ `trust`
105+ Trust level: one of :attr:`.TRUST_ON_FIRST_USE` (default) or
106+ :attr:`.TRUST_SIGNED_CERTIFICATES`.
107+
108+ `user_agent`
109+ A custom user agent string, if required.
110+
74111 """
75112
76113 def __init__ (self , address , ** config ):
@@ -91,13 +128,14 @@ def __init__(self, address, **config):
91128 self .config = config
92129 self .max_pool_size = config .get ("max_pool_size" , DEFAULT_MAX_POOL_SIZE )
93130 self .session_pool = deque ()
94- try :
95- self .encrypted = encrypted = config ["encrypted" ]
96- except KeyError :
131+ encrypted = config .get ("encrypted" , None )
132+ if encrypted is None :
97133 _warn_about_insecure_default ()
98- self .encrypted = encrypted = ENCRYPTED_DEFAULT
134+ encrypted = ENCRYPTION_DEFAULT
135+ self .encrypted = encrypted
99136 self .trust = trust = config .get ("trust" , TRUST_DEFAULT )
100- if encrypted :
137+ if encrypted == ENCRYPTION_ON or \
138+ encrypted == ENCRYPTION_NON_LOCAL and not localhost .match (host ):
101139 if not SSL_AVAILABLE :
102140 raise RuntimeError ("Bolt over TLS is only available in Python 2.7.9+ and Python 3.3+" )
103141 ssl_context = SSLContext (PROTOCOL_SSLv23 )
0 commit comments