MCPcopy Create free account
hub / github.com/PyMySQL/PyMySQL / connect

Method connect

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

Source from the content-addressed store, hash-verified

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

Callers 10

__init__Method · 0.95
pingMethod · 0.95
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