question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

inconsistent results in Python2 - Python3

See original GitHub issue

When I run the following command below. I receive inconsistent results.

Expected Result

The same token should be returned. There is one noticeable difference in the second token which is the b (byte) prefixed to the string.

Actual Result

two different strings

Reproduction Steps

# Python 2.7.15
# Name: PyJWT
# Version: 1.6.4

>>> import jwt
>>> jwt.encode({'transaction': '6fa459ea-ee8a-4ca4-894e-db77e160355e', 'exp': 1497965640}, 'a') 
'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ0cmFuc2FjdGlvbiI6IjZmYTQ1OWVhLWVlOGEtNGNhNC04OTRlLWRiNzdlMTYwMzU1ZSIsImV4cCI6MTQ5Nzk2NTY0MH0.-bpx5A3DfSe3-HO6aH_glS8adcCxUn8lSK1-RPxohhI'

# Python 3.7.0
# Name: PyJWT
# Version: 1.6.4
>>> import jwt
>>> jwt.encode({'transaction': '6fa459ea-ee8a-4ca4-894e-db77e160355e', 'exp': 1497965640}, 'a')
b'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ0cmFuc2FjdGlvbiI6IjZmYTQ1OWVhLWVlOGEtNGNhNC04OTRlLWRiNzdlMTYwMzU1ZSIsImV4cCI6MTQ5Nzk2NTY0MH0.NuqwXlHt27nRxg5W2hHoP5ugnLmw-7QYBumO7lRa1i0'

System Information

$ python -m jwt.help

Tested on Mac OS

{
  "cryptography": {
    "version": "2.2.2"
  },
  "implementation": {
    "name": "CPython",
    "version": "3.7.0"
  },
  "platform": {
    "release": "17.7.0",
    "system": "Darwin"
  },
  "pyjwt": {
    "version": "1.6.4"
  }
}

Tested on Ubuntu 16.04

{
  "cryptography": {
    "version": ""
  },
  "implementation": {
    "name": "CPython",
    "version": "3.5.2"
  },
  "platform": {
    "release": "4.15.13-x86_64-node584",
    "system": "Linux"
  },
  "pyjwt": {
    "version": "1.6.4"
  }
}

This command is only available on PyJWT v1.6.3 and greater. Otherwise, please provide some basic information about your system.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
justmobilizecommented, Jan 3, 2019

The other thing to note is that your data is the same:

eyJ0cmFuc2FjdGlvbiI6IjZmYTQ1OWVhLWVlOGEtNGNhNC04OTRlLWRiNzdlMTYwMzU1ZSIsImV4cCI6MTQ5Nzk2NTY0MH0

It’s the first part, which is the header. eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9 >> b'{"alg":"HS256","typ":"JWT"}' or eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9 >> b'{"typ":"JWT","alg":"HS256"}'

When testing you can use base64.b64decode on the first part (before the first .) and make sure the header is correct

0reactions
macmichael01commented, Jan 3, 2019

@jadhavmanoj I don’t think this will be a common bug that most developers will run into.

I would recommend identifying in your code where PyJWT is being called and run some tests on those specific parts of code both in py2 and py3 and verify that you are receiving expected results.

If you have already developed an app using Flask + PyJWT, I wouldn’t recommend converting to another 3rd party library which wraps PyJWT unless there is a valued added for doing so. Perhaps the value added for the above libraries recommended would help to mitigate the issues that I ran into but then again maybe it wouldn’t. If it doesn’t, converting might cost you more time than it’s worth. The decision is up to you to decide.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Same code for python 2 and 3 gives different results
I have the issue that I run python 3 ...
Read more >
Porting Python 2 Code to Python 3 — Python 3.11.1 ...
In Python 3, 5 / 2 == 2.5 and not 2 ; all division between int values result in a float . This...
Read more >
Common migration problems — Supporting Python 3
Incorrect imports¶ ... Sometimes you'll encounter a situation where 2to3 seems to have missed changing an import. This is usually because you can...
Read more >
Inconsistent invocation of Python versions with pythontex - TeX
1 Answer 1 · I'm using latexmk like this: latexmk -pdf --shell-escape file_with_console_content. · @colidyre You will have to modify how latexmk ...
Read more >
The key differences between Python 2.7.x and Python 3.x with ...
Python 3.x introduced some Python 2-incompatible keywords and ... Based on the timeit results above, you see that the execution for the ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found