NameError: name 'response_data' is not defined

This is the place for queries that don't fit in any of the other categories.

NameError: name 'response_data' is not defined

Postby spikeinc » Tue Aug 13, 2013 9:57 am

Hi I am trying to run this code, basically print "response_data" to a file.

Code: Select all
    request = urllib2.Request('https://10.19.1.1/eispatch.com',post_data)
    request.add_header('Cookie', 'SessId='+sessId)
    response = urllib2.urlopen(request)
    response_data = response.read()
    print response_data

f = open('H:\Work\perl\latest\json.log', 'a')
f.write(response_data)
f.close

however, compiltion gives me below error:
Code: Select all
H:\Work\perl\latest>tcp.py
Traceback (most recent call last):
  File "H:\Work\perl\latest\tcp.py", line 85, in <module>
    f.write(response_data)
NameError: name 'response_data' is not defined


Hmm, looks like I added code tags right after Yoriz did it as well. -- stranac
Last edited by stranac on Tue Aug 13, 2013 12:22 pm, edited 4 times in total.
Reason: Added code tags & changed title
spikeinc
 
Posts: 7
Joined: Fri Aug 09, 2013 9:40 am

Re: NameError: name 'response_data' is not defined

Postby Yoriz » Tue Aug 13, 2013 12:19 pm

I added code tags for you, please go to the new users read this link in my signature.
New Users, Read This
Join the #python-forum IRC channel on irc.freenode.net!
Spam topic disapproval technician
Windows7, Python 2.7.4., WxPython 2.9.5.0., some Python 3.3
User avatar
Yoriz
 
Posts: 871
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

Re: NameError: name 'response_data' is not defined

Postby stranac » Tue Aug 13, 2013 12:24 pm

Going by the indentation of your code, it looks like the beginning of your code is inside a function.
If that's the case, response_data won't exist outside of the function.

You should use return to get the value from the function.
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1152
Joined: Thu Feb 07, 2013 3:42 pm

Re: NameError: name 'response_data' is not defined

Postby spikeinc » Tue Aug 13, 2013 10:21 pm

Thankyou stranac, for pointing out...

I modified the code like this:
But still i get the below error. Any thoughts?

Code: Select all
def tcp_connection_status(ip):
    request = urllib2.Request('https://url,post_data)
    request.add_header('Cookie', 'SessId='+sessId)
    response = urllib2.urlopen(request)
    response_data = response.read()
    print response_data
    return response_data
   
f = open('C:\Python27\json.txt', 'a')
f.write(response_data)
f.close



Code: Select all
Traceback (most recent call last):
  File "C:\Users\tcp.py", line 75, in <module>
    f.write(response_data)
NameError: name 'response_data' is not defined


I am just getting used to indentation in python, its daunting though :( they could have had the use of braces :D
spikeinc
 
Posts: 7
Joined: Fri Aug 09, 2013 9:40 am

Re: NameError: name 'response_data' is not defined

Postby Yoriz » Tue Aug 13, 2013 10:33 pm

Your not actually using the function and getting its return value.
Code: Select all
response_data = tcp_connection_status(ip)
New Users, Read This
Join the #python-forum IRC channel on irc.freenode.net!
Spam topic disapproval technician
Windows7, Python 2.7.4., WxPython 2.9.5.0., some Python 3.3
User avatar
Yoriz
 
Posts: 871
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

Re: NameError: name 'response_data' is not defined

Postby micseydel » Tue Aug 13, 2013 11:02 pm

You need to call close too, this isn't Ruby. It should be
Code: Select all
f.close()

rather than
Code: Select all
f.close
Join the #python-forum IRC channel on irc.freenode.net!

Please do not PM members regarding questions which are meant to be discussed publicly. The point of the forum is so that others can benefit from it. We don't want to help you over PMs or emails.
User avatar
micseydel
 
Posts: 1390
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: NameError: name 'response_data' is not defined

Postby spikeinc » Wed Aug 14, 2013 12:55 am

Yoriz wrote:Your not actually using the function and getting its return value.
Code: Select all
response_data = tcp_connection_status(ip)



Thankyou Yoriz. Got it!
Code: Select all
f = open('C:\Python27\json.txt', 'a')    
while True:
 localtime = time.localtime(time.time())
 response = tcp_connection_status("10.219.136.98")
 print localtime
 #f.write(localtime)
 f.write(response)
 time.sleep (10)   

f.close()
spikeinc
 
Posts: 7
Joined: Fri Aug 09, 2013 9:40 am


Return to General Coding Help

Who is online

Users browsing this forum: No registered users and 5 guests