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

FileType requires the encoding argument to be specified #6

Open
haadr opened this issue Apr 8, 2018 · 3 comments
Open

FileType requires the encoding argument to be specified #6

haadr opened this issue Apr 8, 2018 · 3 comments

Comments

@haadr
Copy link

haadr commented Apr 8, 2018

Hi there and thanks for a great script!

I ran into this today, which was solved by passing encoding="UTF-8" to the FileType constructor used by the metavar FILE (-o file option) and forcing the script to run in python3.

Traceback (most recent call last):
  File "./curl-auth-csrf.py", line 230, in <module>
    main()
  File "./curl-auth-csrf.py", line 217, in main
    args.output.write(result.content.decode('utf-8'))
UnicodeEncodeError: 'ascii' codec can't encode character u'\xe5' in position 182: ordinal not in range(128)```
@JElchison
Copy link
Owner

Hi @haadr, thanks for the report!

Curious, did you happen to try the advice in the Gotchas section of the README? If so, did that assist if your case at all?

@haadr
Copy link
Author

haadr commented Apr 8, 2018

Hi and thanks for the response! I feel embarrassed that I didn't notice that section of the readme, however it doesn't seem to help.

For what it's worth, personally, I don't have a problem simply running it with python3 and specifying the encoding manually/in-code.

However I also don't quite understand the python2-related encoding problem either. Setting PYTHONIOENCODING doesn't seem to help (see below). I can't pretend to have much experience when it comes to UTF-8 and python2/3 compatability, but people seem to recommend importing io to get the encoding argument for the open() function.

Another solution that makes the error go away for me is not trying to decode it from UTF-8 when passing it to the file object for writing (I guess it will then just pass what it got from requests? A quick search didn't tell me whether requests defaults to utf8 in this case...)

diff --git a/curl-auth-csrf.py b/curl-auth-csrf.py
index 2a72fa8..16b9f95 100755
--- a/curl-auth-csrf.py
+++ b/curl-auth-csrf.py
@@ -214,7 +214,7 @@ def main():
         logging.info('Performing GET on %s ...' % url_after_login)
         result = session.get(url_after_login, verify=False, headers={'Referer': result.url, 'User-Agent': args.user_agent_str})
         logging.info("Request result = %d", result.status_code)
-        args.output.write(result.content.decode('utf-8'))
+        args.output.write(result.content)
 
     #########
     # logout

Original error with the environment variable:

21:26:18 bert: curl-auth-csrf $ python2.7 -c "import sys; print sys.stdout.encoding" | cat
None
21:26:26 bert: curl-auth-csrf $ PYTHONIOENCODING=UTF-8 python2.7 -c "import sys; print sys.stdout.encoding" | cat
UTF-8
21:26:43 bert: curl-auth-csrf $ PYTHONIOENCODING=UTF-8 ./curl-auth-csrf.py -i https://<url> -d username=<user> -o user22 https://<url>
Password: 
/usr/local/lib/python2.7/dist-packages/requests/packages/urllib3/connectionpool.py:769: InsecureRequestWarning: Unverified HTTPS request is being made. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.org/en/latest/security.html
  InsecureRequestWarning)
/usr/local/lib/python2.7/dist-packages/requests/packages/urllib3/connectionpool.py:769: InsecureRequestWarning: Unverified HTTPS request is being made. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.org/en/latest/security.html
  InsecureRequestWarning)
/usr/local/lib/python2.7/dist-packages/requests/packages/urllib3/connectionpool.py:769: InsecureRequestWarning: Unverified HTTPS request is being made. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.org/en/latest/security.html
  InsecureRequestWarning)
/usr/local/lib/python2.7/dist-packages/requests/packages/urllib3/connectionpool.py:769: InsecureRequestWarning: Unverified HTTPS request is being made. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.org/en/latest/security.html
  InsecureRequestWarning)
Traceback (most recent call last):
  File "./curl-auth-csrf.py", line 230, in <module>
    main()
  File "./curl-auth-csrf.py", line 217, in main
    args.output.write(result.content.decode('utf-8'))
UnicodeEncodeError: 'ascii' codec can't encode character u'\xe5' in position 186: ordinal not in range(128)
21:33:51 bert: curl-auth-csrf $ echo $PYTHONIOENCODING 
UTF-8

@JElchison
Copy link
Owner

Thanks for the additional info. I'll try to test a few edge cases. I'll report status below when I get to it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants