I'm using the os package to set some environment variable, like this :
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
print ("--------------------------> : ", os.environ)
print '>>>> ',os.getenv('MY_ENV_APP')
if "MY_ENV_APP" not in os.environ:
print "not in !"
os.environ['MY_ENV_APP'] = "prod"
else:
print "ok, good boy !"
print '>>>> ',os.getenv('MY_ENV_APP')
print ("--------------------------> : ", os.environ)
The "MY_ENV_APP" is setting using os.environ, but NOT in Ubuntu system :
...
/usr/local/rvm/bin', 'MY_ENV_APP': 'prod', 'GEM_HOME': '/us
...
>>>> None
not in !
>>>> prod
...
/usr/local/rvm/bin', 'MY_ENV_APP': 'prod', 'GEM_HOME': '/us
...
System os :
[02:59 ]-[vagrant#host]-[/var/www/python]
$ printenv | grep -i my_env_app
[02:59 ]-[vagrant#host]-[/var/www/python]
$ printenv | grep MY_ENV_APP
[03:00 ]-[vagrant#host]-[/var/www/python]
$ echo $MY_ENV_APP
$[03:02 ]-[vagrant#host]-[/var/www/python]
So, the environment variable is setting "in python" but not in the system.
Can you help me please ?
Thanks,
Fabrice
Example:
MY_ENV_APP=abcdef python test.py
This will set the variable for this process only.
Related
Want to verify if site domain contains "com". Assume I have shell varibale as
export FIRST_URL="http://www.11111.com"
export SECOND_URL="http://www.22222.org"
User calls Python script with parameter (partial shell varibale) as
python2.7 FIRST # OR
python2.7 SECOND
Python script is,
import sys, os, subprocess
PART_URL = sys.argv[1]
print( "PART_URL=",PART_URL)
COMPLETE_URL = PART_URL+'_URL' # Formed a full shell varibale
cmd_str='echo {} | grep \"com\".format(COMPLETE_URL)' # equivalent to echo $FIRST_URL | grep com
my_site=subprocess.check_output(cmd_str, shell=True) # Note we cant use subprocess.run() in Python Python 2.7
print("The validated Site is ", my_site)
The output should be "The validated Site is http://www.11111.com"
Refer How to access environment variable values?
$ export FIRST_VAR="http://www.11111.com"
$ python
>>> foo = 'FIRST'
>>> print os.environ[foo + '_VAR']
http://www.11111.com
>>>
Figured it out,
COMPLETE_URL = PART_URL+'_URL'
cmd_str='echo ${} | grep \'com\'.format(COMPLETE_URL)'
my_site=subprocess.check_output(cmd_str, shell=True)
Alteratively, we can use,
my_site=subprocess.call(shlex.split(cmd_str))
Simple script1.py that takes arguments and calls script2.py by passing them to os.system() :
#! /usr/bin/env python
import os
import sys
os.system("script2.py sys.argv[1] sys.argv[2]")
Running this :
./script1.py "arg1" "arg2"
Getting this single error :
sh: 1: script2.py: not found
Both scripts are present in the same directory.
Applied chmod 777 on both script1.py and script2.py and are executable.
Both scripts call the same interpreter installed at /usr/bin/env python.
When I try these :
os.system("./script2.py sys.argv[1] sys.argv[2]")
os.system("python script2.py sys.argv[1] sys.argv[2]")
The sys.argv[1] and sys.argv[2] are being considered as strings instead of dynamic variables
Have you tried with:
./script2.py "arg1" "arg2"
Inside the os.system?
UPDATE 2
Try with:
import urllib
call_with_args = "./script2.py '%s' '%s'" % (str(arg1), str(arg2))
os.system(call_with_args)
# -*- coding: utf-8 -*-
# coding: utf-8
import sys
import os
import time
b = 'sudo tshark -i eth0 -R “tcp contains “attack”” -T fields -e ip.src -a duration:60>output.txt'
a = os.popen(b)
time.sleep(32)
f = open('output.txt','r')
text = 'IP address of attacker is'
print (text), f.read()
f.close
I am trying to execute this code to capture packets using tshark but i am getting this error:
tshark: "�" was unexpected in this context.
Please help me why that error is caused, thank you
The error message is because tshark tries to disable some dangerous functions in Lua like dofile.
If you don't need Lua in tshark, you can disable Lua support: edit init.lua, change disable_lua = false to disable_lua = true.
If you need Lua support, read Platform-Specific information about capture privileges, see how to capture packets without root rivilege.
I'm facing an issue with encoding in running a django app.
I finally found out my django app has no locale set.
The weird thing is that I did set up the envvars file correctly. With this in envvars :
export APACHE_RUN_USER=www-data
export APACHE_RUN_GROUP=www-data
export APACHE_PID_FILE=/var/run/apache2.pid
## The locale used by some modules like mod_dav
export LANG=C
## Uncomment the following line to use the system default locale instead:
. /etc/default/locale
export LANG
locale
When I restart apache the locale command gets executed and I get correct fr_FR.UTF-8 settings for LANG and LC_*.
Now I set up a little test.fcgi script :
#!/usr/bin/python
def myapp(environ, start_response):
start_response('200 OK', [('Content-Type', 'text/plain')])
from commands import getoutput
return ["%s"%getoutput("locale")]
from flup.server.fcgi import WSGIServer
WSGIServer(myapp).run()
when I run it with
sudo -u www-data test.fcgi
I get the correct locale settings as well.
But whenever I access the script through a web browser, I get no locale settings :
LANG=
LC_CTYPE="POSIX"
LC_NUMERIC="POSIX"
LC_TIME="POSIX"
LC_COLLATE="POSIX"
LC_MONETARY="POSIX"
LC_MESSAGES="POSIX"
LC_PAPER="POSIX"
LC_NAME="POSIX"
LC_ADDRESS="POSIX"
LC_TELEPHONE="POSIX"
LC_MEASUREMENT="POSIX"
LC_IDENTIFICATION="POSIX"
LC_ALL=
How come Apache has the right setting but my fcgi script hasn't?
I solved it by adding DefaultInitEnv LANG "en_US.UTF-8" in my sites-available/default. Now the fcgi script tells me UTF-8 !
I would like to create env variables once to use elsewhere in my fabric file. For example:
from fabric.api import *
# environments
def dtconfig():
env.path = 'David'
# tasks
def hello():
require('path', provided_by=[dtconfig])
print (env.path)
print ('Hello $(path)')
print ('Hello ' + env.path)
The output from running 'fab dtconfig hello' is:
David
Hello $(path)
Hello David
Why doesn't the $(path) get replaced with 'David'? thx
Looks like bash variables were removed in later versions of fabric. just used plain old %s string substitutions instead.