Odd output when trying to set up Flask secret key - python

I'm trying to work out how to deploy a Flask app. The docs say I can generate a secret key with a Python command:
python -c 'import os; print(os.urandom(16))'
In their example this outputs b'_5#y2L"F4Q8z\n\xec]/'.
When I run it with python I get odd characters, and with python3 I get character codes. Why are the python and python3 versions different? Which one should I use?
$ python -c 'import os; print(os.urandom(16))'
��L���vl�6��Z5
$ python3 -c 'import os; print(os.urandom(16))'
b'A\xa4\xf3O\xdd\xf4qr\xfb\x9b\x12\x1f*\x0bm\xdf'

You should be using Python 3 for all new projects, so this is essentially a non-issue. The Python 3 output is correct and can be copy pasted directly. The fact that python runs Python 2 for you means you have not followed the tutorial to set up a Python 3 virtualenv, or your virtualenv is not active.
If you're really using Python 2 for some reason, that output is fine too. Copy and paste it into quotes and it will work. Python 2's str is sort-of-bytes, so it outputs non-ASCII characters, while Python 3 always outputs bytes with escape characters (\xAB). Either output will work in either version.
SECRET_KEY = '��L���vl�6��Z5'
SECRET_KEY = b'A\xa4\xf3O\xdd\xf4qr\xfb\x9b\x12\x1f*\x0bm\xdf'
The example output does contain escape characters (\n and \xec), just not as many as the random string you happened to generate.

Related

What does "-c" mean in this linux shell command?

It's from a tutorial on the google cloud platform, they call python from the Linux Shell.
I'm totally new to gcp and linux, please help me, where can I find some exhaustive documentation for python tools in Linux?
python -c 'import base64, sys, json; \
img = base64.b64encode(open(sys.argv[1], "rb").read()); \
print json.dumps({"key":"1", "image_bytes": {"b64": img}})' \
flower1.jpg > request.json
-c stand for cmd which allows you to pass your code as a string.
This feature is available even outside of google-cloud-platform.
you can check other available options using python -h
the -c flag means execute the following command as interpreted by this program.
Doc: https://askubuntu.com/questions/831847/what-is-the-sh-c-command
When called with -c command, it executes the Python statement(s) given as command. Here command may contain multiple statements separated by newlines. Leading whitespace is significant in Python statements!

python3 -m http.server not outputting anything

Need to set up a local server to test out my webpage.But the command is not working and does not give any error message.I can't access http://localhost:8000/ too.
As outlined in the comments, it looks like your PATH variable is mixed up between the commands python (normally used for Python 2) and python3. You can see that python is actually pointing to Python 3 based on your result of python -V, so I would just go with that.
If you want to use python3 as the command for Python 3, the answer depends on how you honestly set up python to be Python 3, but some tips would be:
Reinstall Python 3
Create an alias in your shell profile (alias python3="python")

How to make an exe command run from inside Python? [duplicate]

This question already has answers here:
How to make a call to an executable from Python script?
(3 answers)
Closed 5 years ago.
I have a command like that spins off some calculations and stores some output in a folder. So, I have this command that works on the bash command (in Ubuntu).
/home/usr2/AXE/prog1.exe -k 1 -m /home/usr2/AXE/config.txt
However, I would like to "call" this from inside a python program.
The final idea is to generate lots of serial processes of this nature inside python.
/home/usr2/AXE/prog1.exe -k 1 -m /home/usr2/AXE/config.txt
/home/usr2/AXE/prog1.exe -k 2 -m /home/usr2/AXE/config.txt
/home/usr2/AXE/prog1.exe -k 3 -m /home/usr2/AXE/config.txt
...
...
/home/usr2/AXE/prog1.exe -k 2000 -m /home/usr2/AXE/config.txt
SO FAR
import os
os.system('"/home/usr2/AXE/prog1.exe -k 1 -m /home/usr2/AXE/config.txt"')
This gives me command line output of Out[11]: 32512, but the output file is not produced in the requisite folder.
What am I doing wrong?
P.S. No idea about Bash programming. But would be interested in Bash solutions as well.
The immediate problem seems to be an excess of quotation marks. os.system takes a string containing the command you want to execute. So to execute the shell command echo hello, you would call os.system('echo hello') from Python. Calling os.system('"echo hello"') tries to execute the shell command "echo hello"; in other words, it looks for a single executable file in your PATH named echo hello, which presumably doesn't exist and certainly isn't what you want. So get rid of one set of quotes. Either one is fine since Python treats single and double quotes the same.
You can also try replacing your use of os.system with subprocess.run, also from the standard library. Instead of a single string containing the command, you pass it a sequence of strings, each of which is one token from the shell command you'd like to run. So in our example you could do subprocess.run(('echo', 'hello')). The os.system function is more or less deprecated in favor of subprocess.run.

Output ascii characters to stdout in Python 3

I have a file named 'xxx.py' like this:
print("a simple string")
and when I run that like this (Python 3):
python xxx.py >atextfile.txt
I get a unicode file.
I would like an ascii file.
I don't mind if an exception is thrown if a non-ascii character is attempted to be printed.
What is a simple change I can make to my code that will output ascii characters?
My searches turn up solutions that all seem too verbose for such a simple problem.
[Edit] to report what I learned from setting LC_CTYPE:
I am running on windows 7.
When running on the powershell commandline I get a unicode file (two bytes/character)
When running in a .bat file without LC_CTYPE set I get an ascii file (could be utf-8 as #jwodder pointed out).
When running in a .bat file with LC_CTYPE=ascii set I get presumable an ascii file (1 byte/character).
The stdout encoding is defined by the environment that is executing the python script, e.g.:
$ python -c "import sys; print(sys.stdout.encoding)"
UTF-8
$ LC_CTYPE=ascii python -c "import sys; print(sys.stdout.encoding)"
US-ASCII
Try adjusting your environment before running the script. You can force the encoding value for Python by setting the PYTHONIOENCODING environment variable.

Meaning of ${PYTHON:-python} in shell script and how to change

I am trying to export a Mercurial repo to GitHub using hg-fast-export and Github Bash for Windows. It choked on the line from mercurial import node because mercurial doesn't support Python 3.
I installed Python 2.7 and tried shebang lines (#! /Python27/python) and also alias python='c:/Python27/python'. That worked to make python --version report 2.7, but the hg-fast-export.sh still invokes Python 3 because it contains the line
PYTHON=${PYTHON:-python}
and that evaluates to Python 3.4.3.
Can you explain how to change this to use a different Python version and also what's going on with the syntax here? I couldn't really Google the meaning of ${} or :- in the shell. Comments on how likely my approach is to get this running on Windows could also be helpful.
Edit: Thanks for the explanations of :-. Since the parameter expansion was not needed, I guess the answer to my question was "You have to set PYTHON='c:/Python27/python' in the same line as the script for it to use that value." I expected it to be like PATH where you can set it independently for following lines to use.
The intent here is to allow an override to be passed in through the environment.
Thus, if you run at a POSIX shell:
$ PYTHON=python26 hg-fast-export ...
then in hg-fast-export will evaluate ${PYTHON:-python} to python26.
That syntax evaluates a variable, but provides a default:
$ foo=123
$ echo ${foo:-456}
123
$ echo ${bar:-456}
456
You could try to pass a modified $PYTHON to the script:
$ PYTHON=c:/Python27/python hg-fast-export.sh ...
This is a way to evaluate and modify text (parameter expansion). Consider this example:
$ PYTHON="/usr/bin/python --version"
$ ${PYTHON:-python}
Python 2.7.10
PYTHON is originally the path and command on how to evaluate the version.
${PYTHON:-python} evaluates and runs the former, but it was not empty so the colon dash is not needed
For a detailed breakdown see What does the colon dash ":-" mean in bash

Categories