1616# under the License.
1717
1818import sys
19+ import os
20+ import ssl
21+ import socket
1922
2023sys .path .append (".." )
2124
2225from gremlin_python .process .anonymous_traversal import traversal
2326from gremlin_python .process .strategies import *
2427from gremlin_python .driver .driver_remote_connection import DriverRemoteConnection
2528from gremlin_python .driver .serializer import GraphBinarySerializersV1
29+ from gremlin_python .driver .aiohttp .transport import AiohttpTransport
2630
31+ VERTEX_LABEL = os .getenv ('VERTEX_LABEL' , 'connection' )
2732
2833def main ():
2934 with_remote ()
@@ -40,15 +45,14 @@ def with_remote():
4045 #
4146 # which starts it in "console" mode with an empty in-memory TinkerGraph ready to go bound to a
4247 # variable named "g" as referenced in the following line.
43- rc = DriverRemoteConnection ('ws://localhost:8182/gremlin' , 'g' )
48+ # if there is a port placeholder in the env var then we are running with docker so set appropriate port
49+ server_url = os .getenv ('GREMLIN_SERVER_URL' , 'ws://localhost:8182/gremlin' ).format (45940 )
50+ rc = DriverRemoteConnection (server_url , 'g' )
4451 g = traversal ().with_remote (rc )
4552
46- # drop existing vertices
47- g .V ().drop ().iterate ()
48-
4953 # simple query to verify connection
50- v = g .add_v ().iterate ()
51- count = g .V ().count ().next ()
54+ v = g .add_v (VERTEX_LABEL ).iterate ()
55+ count = g .V ().has_label ( VERTEX_LABEL ). count ().next ()
5256 print ("Vertex count: " + str (count ))
5357
5458 # cleanup
@@ -57,41 +61,60 @@ def with_remote():
5761
5862# connecting with plain text authentication
5963def with_auth ():
60- rc = DriverRemoteConnection ('ws://localhost:8182/gremlin' , 'g' , username = 'stephen' , password = 'password' )
64+ # if there is a port placeholder in the env var then we are running with docker so set appropriate port
65+ server_url = os .getenv ('GREMLIN_SERVER_BASIC_AUTH_URL' , 'ws://localhost:8182/gremlin' ).format (45941 )
66+
67+ # disable SSL certificate verification for CI environments
68+ if ':45941' in server_url :
69+ ssl_opts = ssl .SSLContext (ssl .PROTOCOL_TLS_CLIENT )
70+ ssl_opts .check_hostname = False
71+ ssl_opts .verify_mode = ssl .CERT_NONE
72+ rc = DriverRemoteConnection (server_url , 'g' , username = 'stephen' , password = 'password' ,
73+ transport_factory = lambda : AiohttpTransport (ssl_options = ssl_opts ))
74+ else :
75+ rc = DriverRemoteConnection (server_url , 'g' , username = 'stephen' , password = 'password' )
76+
6177 g = traversal ().with_remote (rc )
6278
63- v = g .add_v ().iterate ()
64- count = g .V ().count ().next ()
79+ v = g .add_v (VERTEX_LABEL ).iterate ()
80+ count = g .V ().has_label ( VERTEX_LABEL ). count ().next ()
6581 print ("Vertex count: " + str (count ))
6682
6783 rc .close ()
6884
6985
7086# connecting with Kerberos SASL authentication
7187def with_kerberos ():
72- rc = DriverRemoteConnection (
'ws://localhost:8182/gremlin' ,
'g' ,
kerberized_service = '[email protected] ' )
88+ # if there is a port placeholder in the env var then we are running with docker so set appropriate port
89+ server_url = os .getenv ('GREMLIN_SERVER_URL' , 'ws://localhost:8182/gremlin' ).format (45942 )
90+ kerberos_hostname = os .getenv ('KRB_HOSTNAME' , socket .gethostname ())
91+ kerberized_service = f'test-service@{ kerberos_hostname } '
92+
93+ rc = DriverRemoteConnection (server_url , 'g' , kerberized_service = kerberized_service )
7394 g = traversal ().with_remote (rc )
7495
75- v = g .add_v ().iterate ()
76- count = g .V ().count ().next ()
96+ v = g .add_v (VERTEX_LABEL ).iterate ()
97+ count = g .V ().has_label ( VERTEX_LABEL ). count ().next ()
7798 print ("Vertex count: " + str (count ))
7899
79100 rc .close ()
80101
81102
82103# connecting with customized configurations
83104def with_configs ():
105+ # if there is a port placeholder in the env var then we are running with docker so set appropriate port
106+ server_url = os .getenv ('GREMLIN_SERVER_URL' , 'ws://localhost:8182/gremlin' ).format (45940 )
84107 rc = DriverRemoteConnection (
85- 'ws://localhost:8182/gremlin' , 'g' ,
108+ server_url , 'g' ,
86109 username = "" , password = "" , kerberized_service = '' ,
87110 message_serializer = GraphBinarySerializersV1 (), graphson_reader = None ,
88111 graphson_writer = None , headers = None , session = None ,
89112 enable_user_agent_on_connect = True
90113 )
91114 g = traversal ().with_remote (rc )
92115
93- v = g .add_v ().iterate ()
94- count = g .V ().count ().next ()
116+ v = g .add_v (VERTEX_LABEL ).iterate ()
117+ count = g .V ().has_label ( VERTEX_LABEL ). count ().next ()
95118 print ("Vertex count: " + str (count ))
96119
97120 rc .close ()
0 commit comments