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

Method connect

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

Source from the content-addressed store, hash-verified

661 self.collation = collation
662
663 def connect(self, sock=None):
664 self._closed = False
665 try:
666 if sock is None:
667 if self.unix_socket:
668 sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
669 sock.settimeout(self.connect_timeout)
670 sock.connect(self.unix_socket)
671 self.host_info = "Localhost via UNIX socket"
672 self._secure = True
673 if DEBUG:
674 print("connected using unix_socket")
675 else:
676 kwargs = {}
677 if self.bind_address is not None:
678 kwargs["source_address"] = (self.bind_address, 0)
679 while True:
680 try:
681 sock = socket.create_connection(
682 (self.host, self.port), self.connect_timeout, **kwargs
683 )
684 break
685 except OSError as e:
686 if e.errno == errno.EINTR:
687 continue
688 raise
689 self.host_info = "socket %s:%d" % (self.host, self.port)
690 if DEBUG:
691 print("connected using socket")
692 sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
693 sock.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1)
694
695 self._sock = sock
696 sock.settimeout(self._read_timeout)
697 self._current_timeout = self._read_timeout
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 charset (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()

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