need help running python script in linux

X

xunil

hi, could any one please help show me how to run a python script in
linux ?

when i try "python script.py" it just does nothing

this is the script iam trying to run


#!/usr/bin/env python
"USAGE: echoserver.py <port>"
from SocketServer import BaseRequestHandler, TCPServer
import sys, socket

class EchoHandler(BaseRequestHandler):
def handle(self):
print "Client connected:", self.client_address
self.request.sendall(self.request.recv(2**16))
self.request.close()

if len(sys.argv) != 2:
print __doc__
else:
TCPServer(('',int(sys.argv[1])),
EchoHandler).serve_forever()


this is code for a server socket , iam supposed to see some messages
when i run the script but nothing shows completely

even the client script reacts the same way
below is the client code

#!/usr/bin/env python
"USAGE: echoclient.py <server> <word> <port>"
from socket import * # import *, but we'll avoid name conflict
import sys
if len(sys.argv) != 4:
print __doc__
sys.exit(0)
sock = socket(AF_INET, SOCK_STREAM)
sock.connect((sys.argv[1], int(sys.argv[3])))
message = sys.argv[2]
messlen, received = sock.send(message), 0
if messlen != len(message):
print "Failed to send complete message"
print "Received: ",
while received < messlen:
data = sock.recv(32)
sys.stdout.write(data)
received += len(data)
print
sock.close()


i have the python interpreter installed already
but i don't know why these failed to run.

could some one please come to my rescue
 
L

Lutz Horn

xunil said:
when i try "python script.py" it just does nothing
#!/usr/bin/env python
"USAGE: echoserver.py <port>"
from SocketServer import BaseRequestHandler, TCPServer
import sys, socket

class EchoHandler(BaseRequestHandler):
def handle(self):
print "Client connected:", self.client_address
self.request.sendall(self.request.recv(2**16))
self.request.close()

if len(sys.argv) != 2:
print __doc__
else:
TCPServer(('',int(sys.argv[1])), EchoHandler).serve_forever()

What's this? This probably shouldn't be inside of handle.

If you just run this script, the class EchoHandler will be created but
no instance of it will ever be created. You must add some code outside
the class definition that acutally does something.

class EchoHandler(BaseRequestHandler):
def handle(self):
print "Client connected:", self.client_address
self.request.sendall(self.request.recv(2**16))
self.request.close()

if __name__ == "__main__":
if len(sys.argv) != 2:
print __doc__
else:
TCPServer(('', int(sys.argv[1])),
EchoHandler).serve_forever()
 
R

Ryan Paul

#!/usr/bin/env python
"USAGE: echoserver.py <port>"
from SocketServer import BaseRequestHandler, TCPServer
import sys, socket

class EchoHandler(BaseRequestHandler):
def handle(self):
print "Client connected:", self.client_address
self.request.sendall(self.request.recv(2**16))
self.request.close()

if len(sys.argv) != 2:
print __doc__
else:
TCPServer(('',int(sys.argv[1])),
EchoHandler).serve_forever()

this defines a class, but you dont instantiate the class- so the code
essentially doesn't do anything. I suggest you read some tutorials.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top