Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AttributeError: 'bytes' object has no attribute 'encode' #64

Closed
petehunt opened this issue Jul 27, 2011 · 7 comments
Closed

AttributeError: 'bytes' object has no attribute 'encode' #64

petehunt opened this issue Jul 27, 2011 · 7 comments

Comments

@petehunt
Copy link
Contributor

Originally filed by dhj...@gmail.com on 2011-05-24T21:33:19

Using the example program on the home page (with Python3), I tried to access a database on a remote linux server. The password was assigned a valid string (rather than None).

In connections.py, I'm seeing the error described in summary.
It is happening in the second argument to _scamble_323, it does not seem to like trying to execute the method 'encode' on the object variable 'salt'. I have checked and 'salt' does have a legitimate binary value.

However, my Python is not yet good enough for me to be able to debug this issue for myself.

Thanks in advance

@petehunt
Copy link
Contributor Author

Updated by CTimmerman2 on 2011-07-27T16:03:29

Same here. It worked fine before; I don't know what broke it.

import pymysql; pymysql.connect(host="127.0.0.1", port=3306, user="remoteUser", passwd="secret", db="myportal")
Traceback (most recent call last):
File "", line 1, in
File "C:\python32\lib\site-packages\pymysql__init__.py", line 93, in Connect
return Connection(_args, *_kwargs)
File "C:\python32\lib\site-packages\pymysql\connections.py", line 513, in init
self._connect()
File "C:\python32\lib\site-packages\pymysql\connections.py", line 667, in _connect
self._request_authentication()
File "C:\python32\lib\site-packages\pymysql\connections.py", line 720, in _request_authentication
self._send_authentication()
File "C:\python32\lib\site-packages\pymysql\connections.py", line 776, in _send_authentication
data = _scramble_323(self.password.encode(self.charset), self.salt.encode(self.charset)) + int2byte(0)
AttributeError: 'bytes' object has no attribute 'encode'
import sys; sys.version
'3.2 (r32:88445, Feb 20 2011, 21:29:02) [MSC v.1500 32 bit (Intel)]'
import platform; platform.uname()
('Windows', 'C-GXYRJ4J', '7', '6.1.7600', 'AMD64', 'Intel64 Family 6 Model 26 Stepping 5, GenuineIntel')

@petehunt
Copy link
Contributor Author

Updated by CTimmerman2 on 2011-07-27T16:09:33

pymysql.version_info
(1, 2, 2, 'final', 0)
pymysql.VERSION
(0, 4, None)

Installed (again) from PyMySQL3-0.4.tar.gz:

C:\home\CT\PyMySQL3-0.4>c:\python32\python setup.py install
running install
running build
running build_py
creating build
creating build\lib
creating build\lib\pymysql
copying pymysql\charset.py -> build\lib\pymysql
copying pymysql\connections.py -> build\lib\pymysql
copying pymysql\converters.py -> build\lib\pymysql
copying pymysql\cursors.py -> build\lib\pymysql
copying pymysql\err.py -> build\lib\pymysql
copying pymysql\times.py -> build\lib\pymysql
copying pymysql\util.py -> build\lib\pymysql
copying pymysql__init__.py -> build\lib\pymysql
creating build\lib\pymysql\constants
copying pymysql\constants\CLIENT.py -> build\lib\pymysql\constants
copying pymysql\constants\COMMAND.py -> build\lib\pymysql\constants
copying pymysql\constants\ER.py -> build\lib\pymysql\constants
copying pymysql\constants\FIELD_TYPE.py -> build\lib\pymysql\constants
copying pymysql\constants\FLAG.py -> build\lib\pymysql\constants
copying pymysql\constants\SERVER_STATUS.py -> build\lib\pymysql\constants
copying pymysql\constants__init__.py -> build\lib\pymysql\constants
creating build\lib\pymysql\tests
copying pymysql\tests\base.py -> build\lib\pymysql\tests
copying pymysql\tests\test_basic.py -> build\lib\pymysql\tests
copying pymysql\tests\test_example.py -> build\lib\pymysql\tests
copying pymysql\tests\test_issues.py -> build\lib\pymysql\tests
copying pymysql\tests__init__.py -> build\lib\pymysql\tests
running install_lib
running install_egg_info
Removing c:\python32\Lib\site-packages\PyMySQL3-0.4-py3.2.egg-info
Writing c:\python32\Lib\site-packages\PyMySQL3-0.4-py3.2.egg-info

@robisonsantos
Copy link

I had the same problem, and looking at the code I found:

----------------------------------------- (line 855)

    if auth_packet.is_eof_packet():
        # send legacy handshake
        #raise NotImplementedError, "old_passwords are not supported. Check to see if mysqld was started with --old-passwords, if old-passwords=1 in a my.cnf file, or if there are some short hashes in your mysql.user table."
        # TODO: is this the correct charset?
        data = _scramble_323(self.password.encode(self.charset), self.salt.encode(self.charset)) + int2byte(0)

at this point self.data is a bytes var, which does not have an encode method.
I just removed the encode method call and everything worked fine

@simon04
Copy link

simon04 commented Mar 14, 2013

For me this problem was due to the usage of the old authentication method (cf. http://dev.mysql.com/doc/internals/en/authentication-method.html). Creating a new user (resetting the password might work as well) and making sure that the password column for this user in the mysql.user table starts with a * (otherwise check the server configuration according to the above link), solved the problem for me.

@wyndhblb
Copy link

Seems now altering this

data = _scramble_323(self.password.encode(self.charset), self.salt.encode(self.charset)) + b'\0'

to this

data = _scramble_323(self.password, self.salt) + b'\0'

for MySQL 5.0.77 at least and django trunk (as of today) mends the issue .. i.e. things are already Bytes

@fuzzy-marmot
Copy link

That actually didn't work for me - I only needed to get rid of the encode after self.salt, not self.password. If I get rid of both, I get further downstream errors. Regardless, this seems to be a very easy fix. Is there a way for one of us to commit the change back to the repository?

@methane
Copy link
Member

methane commented Oct 26, 2013

Fixed via 1a24311 .

@methane methane closed this as completed Oct 26, 2013
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 7, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants