rodrwan

Ejemplo cliente ayudantia 1

Aug 17th, 2012
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.08 KB | None | 0 0
  1. #!/usr/bin/python
  2.  
  3. # RECUERDEN LA IDENTACION SI IMPORTA.
  4. # NO ES LO MISMO UN TAB QUE UN ESPACIO.
  5. # OPTEN SIEMPRE POR UNO NO LOS DOS.
  6.  
  7. import socket  # biblioteca con todo lo necesario para manejar sockets
  8.  
  9. sc = socket.socket()  # instanciamos un objeto del tipo socket
  10. sc.connect(("localhost", 9998))  #fijamos la ip y el puerto donde el cliente se conectara
  11.  
  12. def nombre_funcion(mensaje): #definimos una funcion
  13.     return mensaje
  14.    
  15. while True: # while infinito para interactuar constantemente con el servidor
  16.     num = raw_input("Primer numero: ")  # recibimos por teclado
  17.     if num == "quit":  
  18.         sc.send(num)
  19.         break  
  20.     op = raw_input("Operador: ")
  21.     num2 = raw_input("Segundo numero: ")
  22.     mensaje = num + " " + op + " " + num2
  23.     print nombre_funcion(mensaje)
  24.     sc.send(mensaje) #enviamos mensaje al servidor
  25.     resp = sc.recv(1024)  # recibimos respuesta del servidor
  26.     print "Resultado %d" % int(resp) #imprimimos respuesta del servidor
  27.      
  28.  
  29. print "adios"  
  30. sc.shutdown(socket.SHUT_RD | socket.SHUT_WR)
  31. sc.close() #cerramos la conexion
Advertisement
Add Comment
Please, Sign In to add comment