Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python KeyError while running script

Tags:

python-3.3

I am running the below python script.

import cgi
import os
import time
import sys
import yate

print(yate.start_response('text/plain'))
addr=os.environ['REMOTE_ADDR']
host=os.environ['REMOTE_HOST']
method=os.environ['REQUEST_METHOD']
cur_time=time.asctime(time.localtime())
print(host+","+addr+","+cur_time+":"+method+":",end='',file=sys.stderr)

and i am getting the below error.

    addr=os.environ['REMOTE_ADDR']
  File "C:\Python33\lib\os.py", line 676, in __getitem__
    raise KeyError(key) from None
KeyError: 'REMOTE_ADDR'

Please help on this....

like image 861
subin alex Avatar asked Apr 21 '26 04:04

subin alex


1 Answers

Your script is supposed to be run as a CGI script by a web-server, which sets environment variables like REMOTE_ADDR, REQUEST_METHOD, etc.

You are running the script by yourself, and these environment variables are not available. That's why you get the KeyError.

like image 78
warvariuc Avatar answered Apr 24 '26 21:04

warvariuc



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!