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

Method connect

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

Source from the content-addressed store, hash-verified

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