PyMySQL和Socket一起开始

时间:2017-08-23 19:23:42

标签: python-3.x sockets pymysql

我开始使用Socket:

sock = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
sock.bind(('0.0.0.0', 50011))
sock.listen(1)
sock.setblocking(1)
c, a = sock.accept()

如果客户端连接, 然后:

connection = pymysql.connect...

然后崩溃。

两个模块不能一起工作吗? 我需要Socket和pymysql。 我还尝试让两个模块在一个线程上单独工作。不幸的是,这不起作用。我仍然得到同样的错误。

错误:

Exception in thread Thread-2:
Traceback (most recent call last):
  File "C:\Users\Erik\Desktop\AT Programmierung\Python\Kommunikation Server-Client\pymysql\connections.py", line 916, in connect
    **kwargs)
  File "C:\Users\Erik\AppData\Local\Programs\Python\Python36\lib\socket.py", line 704, in create_connection
    for res in getaddrinfo(host, port, 0, SOCK_STREAM):
  File "C:\Users\Erik\AppData\Local\Programs\Python\Python36\lib\socket.py", line 743, in getaddrinfo
    for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno 11001] getaddrinfo failed

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\Erik\AppData\Local\Programs\Python\Python36\lib\threading.py", line 916, in _bootstrap_inner
    self.run()
  File "C:\Users\Erik\AppData\Local\Programs\Python\Python36\lib\threading.py", line 864, in run
    self._target(*self._args, **self._kwargs)
  File "C:\Users\Erik\Desktop\AT Programmierung\Python\Kommunikation Server-Client\ServerThread.py", line 23, in mysql
    cursorclass=pymysql.cursors.DictCursor)
  File "C:\Users\Erik\Desktop\AT Programmierung\Python\Kommunikation Server-Client\pymysql\__init__.py", line 90, in Connect
    return Connection(*args, **kwargs)
  File "C:\Users\Erik\Desktop\AT Programmierung\Python\Kommunikation Server-Client\pymysql\connections.py", line 706, in __init__
    self.connect()
  File "C:\Users\Erik\Desktop\AT Programmierung\Python\Kommunikation Server-Client\pymysql\connections.py", line 963, in connect
    raise exc
pymysql.err.OperationalError: (2003, "Can't connect to MySQL server on 'dj-eki.de:3306' ([Errno 11001] getaddrinfo failed)")

1 个答案:

答案 0 :(得分:0)

我最初尝试使用以下方式进行连接:

connection = pymysql.connect(host='dj-eki.de:3306',

然而,这是错误的,并没有奏效。

相反,我需要将端口与主机分开,如下所示:

connection = pymysql.connect(host='dj-eki.de',
                             port=3306,