2017-08-23 51 views
0

我開始一個Socket: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)") 

回答

0

我最初嘗試使用連接:

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

但是,這是錯誤的,是行不通的。

相反,我需要的端口從主機分開,就像這樣:

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