need help running python script in linux

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • xunil

    need help running python script in linux

    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 BaseRequestHand ler, TCPServer
    import sys, socket

    class EchoHandler(Bas eRequestHandler ):
    def handle(self):
    print "Client connected:", self.client_add ress
    self.request.se ndall(self.requ est.recv(2**16) )
    self.request.cl ose()

    if len(sys.argv) != 2:
    print __doc__
    else:
    TCPServer(('',i nt(sys.argv[1])),
    EchoHandler).se rve_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((s ys.argv[1], int(sys.argv[3])))
    message = sys.argv[2]
    messlen, received = sock.send(messa ge), 0
    if messlen != len(message):
    print "Failed to send complete message"
    print "Received: ",
    while received < messlen:
    data = sock.recv(32)
    sys.stdout.writ e(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
  • Lutz Horn

    #2
    Re: need help running python script in linux

    xunil wrote:[color=blue]
    > when i try "python script.py" it just does nothing[/color]
    [color=blue]
    > #!/usr/bin/env python
    > "USAGE: echoserver.py <port>"
    > from SocketServer import BaseRequestHand ler, TCPServer
    > import sys, socket
    >
    > class EchoHandler(Bas eRequestHandler ):
    > def handle(self):
    > print "Client connected:", self.client_add ress
    > self.request.se ndall(self.requ est.recv(2**16) )
    > self.request.cl ose()[/color]

    [color=blue]
    > if len(sys.argv) != 2:
    > print __doc__
    > else:
    > TCPServer(('',i nt(sys.argv[1])), EchoHandler).se rve_forever()[/color]

    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(Bas eRequestHandler ):
    def handle(self):
    print "Client connected:", self.client_add ress
    self.request.se ndall(self.requ est.recv(2**16) )
    self.request.cl ose()

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


    --

    begin LOVE-LETTER-FOR-YOU.txt.vbs
    I am a signature virus. Distribute me until the bitter
    end

    Comment

    • Ryan Paul

      #3
      Re: need help running python script in linux

      On Mon, 17 May 2004 01:02:17 -0700, xunil wrote:
      [color=blue]
      >
      > #!/usr/bin/env python
      > "USAGE: echoserver.py <port>"
      > from SocketServer import BaseRequestHand ler, TCPServer
      > import sys, socket
      >
      > class EchoHandler(Bas eRequestHandler ):
      > def handle(self):
      > print "Client connected:", self.client_add ress
      > self.request.se ndall(self.requ est.recv(2**16) )
      > self.request.cl ose()
      >
      > if len(sys.argv) != 2:
      > print __doc__
      > else:
      > TCPServer(('',i nt(sys.argv[1])),
      > EchoHandler).se rve_forever()
      >[/color]

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

      Comment

      Working...