MCPcopy
hub / github.com/PyMySQL/PyMySQL / connect

Method connect

pymysql/connections.py:664–749  ·  view source on GitHub ↗
(self, sock=None)

Source from the content-addressed store, hash-verified

662 self.collation = collation
663
664 def connect(self, sock=None):
665 self._closed = False
666 try:
667 if sock is None:
668 if self.unix_socket:
669 sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
670 sock.settimeout(self.connect_timeout)
671 sock.connect(self.unix_socket)
672 self.host_info = "Localhost via UNIX socket"
673 self._secure = True
674 if DEBUG:
675 print("connected using unix_socket")
676 else:
677 kwargs = {}
678 if self.bind_address is not None:
679 kwargs["source_address"] = (self.bind_address, 0)
680 while True:
681 try:
682 sock = socket.create_connection(
683 (self.host, self.port), self.connect_timeout, **kwargs
684 )
685 break
686 except OSError as e:
687 if e.errno == errno.EINTR:
688 continue
689 raise
690 self.host_info = "socket %s:%d" % (self.host, self.port)
691 if DEBUG:
692 print("connected using socket")
693 sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
694 sock.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1)
695 sock.settimeout(None)
696
697 self._sock = sock
698 self._rfile = sock.makefile("rb")
699 self._next_seq_id = 0
700
701 self._get_server_information()
702 self._request_authentication()
703
704 # Send "SET NAMES" query on init for:
705 # - Ensure charaset (and collation) is set to the server.
706 # - collation_id in handshake packet may be ignored.
707 # - If collation is not specified, we don't know what is server's
708 # default collation for the charset. For example, default collation
709 # of utf8mb4 is:
710 # - MySQL 5.7, MariaDB 10.x: utf8mb4_general_ci
711 # - MySQL 8.0: utf8mb4_0900_ai_ci
712 #
713 # Reference:
714 # - https://github.com/PyMySQL/PyMySQL/issues/1092
715 # - https://github.com/wagtail/wagtail/issues/9477
716 # - https://zenn.dev/methane/articles/2023-mysql-collation (Japanese)
717 self.set_character_set(self.charset, self.collation)
718
719 if self.sql_mode is not None:
720 c = self.cursor()
721 c.execute("SET sql_mode=%s", (self.sql_mode,))

Callers 11

__init__Method · 0.95
pingMethod · 0.95
example.pyFile · 0.45
test_sha256_no_passwordFunction · 0.45
test_sha256_passwordFunction · 0.45
test_sha256_password_sslFunction · 0.45

Calls 8

set_character_setMethod · 0.95
cursorMethod · 0.95
autocommitMethod · 0.95
_force_closeMethod · 0.95
executeMethod · 0.80
closeMethod · 0.45