43
43
from .utils import _ConnectionContextManager , _ContextManager
44
44
# from .log import logger
45
45
46
+
46
47
DEFAULT_USER = getpass .getuser ()
47
48
48
49
@@ -90,7 +91,8 @@ def __init__(self, host="localhost", user=None, password="",
90
91
client_flag = 0 , cursorclass = Cursor , init_command = None ,
91
92
connect_timeout = None , read_default_group = None ,
92
93
no_delay = None , autocommit = False , echo = False ,
93
- local_infile = False , loop = None , ssl = None , auth_plugin = '' ):
94
+ local_infile = False , loop = None , ssl = None , auth_plugin = '' ,
95
+ program_name = '' ):
94
96
"""
95
97
Establish a connection to the MySQL database. Accepts several
96
98
arguments:
@@ -165,6 +167,17 @@ def __init__(self, host="localhost", user=None, password="",
165
167
self ._server_auth_plugin = ""
166
168
self ._auth_plugin_used = ""
167
169
170
+ # TODO somehow import version from __init__.py
171
+ self ._connect_attrs = {
172
+ '_client_name' : 'aiomysql' ,
173
+ '_pid' : str (os .getpid ()),
174
+ '_client_version' : '0.0.16' ,
175
+ }
176
+ if False or program_name :
177
+ self ._connect_attrs ["program_name" ] = program_name
178
+ elif sys .argv :
179
+ self ._connect_attrs ["program_name" ] = sys .argv [0 ]
180
+
168
181
self ._unix_socket = unix_socket
169
182
if charset :
170
183
self ._charset = charset
@@ -672,8 +685,10 @@ async def _request_authentication(self):
672
685
charset_id = charset_by_name (self .charset ).id
673
686
if isinstance (self .user , str ):
674
687
_user = self .user .encode (self .encoding )
688
+ else :
689
+ _user = self .user
675
690
676
- data_init = struct .pack ('<iIB23s' , self .client_flag , 1 ,
691
+ data_init = struct .pack ('<iIB23s' , self .client_flag , MAX_PACKET_LEN ,
677
692
charset_id , b'' )
678
693
679
694
data = data_init + _user + b'\0 '
@@ -686,8 +701,8 @@ async def _request_authentication(self):
686
701
auth_plugin = self ._server_auth_plugin
687
702
688
703
if auth_plugin in ('' , 'mysql_native_password' ):
689
- authresp = _auth .scramble_native_password (self . _password ,
690
- self .salt )
704
+ authresp = _auth .scramble_native_password (
705
+ self . _password . encode ( 'latin1' ), self .salt )
691
706
elif auth_plugin in ('' , 'mysql_clear_password' ):
692
707
authresp = self ._password .encode ('latin1' ) + b'\0 '
693
708
@@ -715,6 +730,15 @@ async def _request_authentication(self):
715
730
716
731
self ._auth_plugin_used = auth_plugin
717
732
733
+ if self .server_capabilities & CLIENT .CONNECT_ATTRS :
734
+ connect_attrs = b''
735
+ for k , v in self ._connect_attrs .items ():
736
+ k = k .encode ('utf8' )
737
+ connect_attrs += struct .pack ('B' , len (k )) + k
738
+ v = v .encode ('utf8' )
739
+ connect_attrs += struct .pack ('B' , len (v )) + v
740
+ data += struct .pack ('B' , len (connect_attrs )) + connect_attrs
741
+
718
742
self .write_packet (data )
719
743
auth_packet = await self ._read_packet ()
720
744
@@ -732,7 +756,8 @@ async def _request_authentication(self):
732
756
else :
733
757
# send legacy handshake
734
758
data = _auth .scramble_old_password (
735
- self ._password , auth_packet .read_all ()) + b'\0 '
759
+ self ._password .encode ('latin1' ),
760
+ auth_packet .read_all ()) + b'\0 '
736
761
self .write_packet (data )
737
762
auth_packet = await self ._read_packet ()
738
763
@@ -741,12 +766,13 @@ async def _process_auth(self, plugin_name, auth_packet):
741
766
# https://dev.mysql.com/doc/internals/en/
742
767
# secure-password-authentication.html#packet-Authentication::
743
768
# Native41
744
- data = _auth .scramble_native_password (self ._password ,
745
- auth_packet .read_all ())
769
+ data = _auth .scramble_native_password (
770
+ self ._password .encode ('latin1' ),
771
+ auth_packet .read_all ())
746
772
elif plugin_name == b"mysql_old_password" :
747
773
# https://dev.mysql.com/doc/internals/en/
748
774
# old-password-authentication.html
749
- data = _auth .scramble_old_password (self ._password ,
775
+ data = _auth .scramble_old_password (self ._password . encode ( 'latin1' ) ,
750
776
auth_packet .read_all ()) + b'\0 '
751
777
elif plugin_name == b"mysql_clear_password" :
752
778
# https://dev.mysql.com/doc/internals/en/
0 commit comments