I tried executing a simple line drawing program using exec().
It worked fine. But when I tried to execute the same program by sending it through a tcp/ip network(the server reads the program and sends it to the client which receives it to a variable(b) of string type) and then i use exec(b) in the client to execute it but it says:
NameError: global name 'display' is not defined

The line drawing code is:

from OpenGL.GLUT import *
from OpenGL.GLU import *
from OpenGL.GL import *
import sys

name = 'line'

def display():
	glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)
	glPushMatrix()
	glTranslatef(-1,-1,0)
	gluLookAt(
		0.1, 0.1, 0.3,
		0.0, 0.0, 0.0,
		0.0, 1.0, 0.0);

	glLineWidth(3.0)
	color = [1.,1.,1.,1.]
	glBegin(GL_LINES)
	glVertex3f(0,0,0) # origin of the line
	glVertex3f(.5,1.0,.9) # ending point of the line
	glEnd()
	glPopMatrix()
	glutSwapBuffers()
	return
def main():
	glutInit(sys.argv)
	print 'hello'
	glutCreateWindow(name)
	glClearColor(0.4,0.5,0.3,1.0)
	glutDisplayFunc(display)
	glutMainLoop()
	return
main()
 
This part of the client code receives the program and stores it into the variable and then we use exec():

while f:	
			a = client.recv(1024)
			if a=="#p":
				f=0
				break
			b+=a
			
		print b
		
		exec(b)

The code executes upto the part where print hello is given and then stops.

The error message:
hello
Exception in thread Thread-1:
Traceback (most recent call last):
File "/usr/lib/python2.6/threading.py", line 532, in __bootstrap_inner
self.run()
File "r13client.py", line 31, in run
exec(b)
File "<string>", line 34, in <module>
File "<string>", line 31, in main
NameError: global name 'display' is not defined
I am unable to understand what is going wrong here. If anyone could help I'd be grateful.

Recommended Answers

All 3 Replies

Try this

D = dict()
exec b in D

Also learn about code tags here http://www.daniweb.com/forums/announcement.php?f=8&announcementid=3 :)

Also note that network security rules prohibit using 'exec' on a piece of code received from a public network. This may potentially run arbitrary code on your computer.

Well i think your code here is not complete to trace all the errors.
But The global name display is a method . so do this where ever you call the display method.

display() # This will call a function dispaly(). Note that display is not the same as display().

Happy coding :)

Thnx to all for help...i defined the function display in a class,say d, and stored this class to a global variable (say x).
x= d()
this variable now acts as the object of the class and i called the function display using this object (x.display). The program worked!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.