Needing help with sockets

Martin Dion dion_mart at hotmail.com
Mon Jul 21 01:48:57 EDT 2003


Hi

i'm relatively new to python and I need help with a little program i 
wrote

What i want to do:

I want to be able to connect on any port on a system and then send and 
receive commands, by following the protocol rules.

If I receive data, it must print on the screen.

It asks for data to send in reply to the data received.

Once data is sent, it must look if there is data to receive and if not, 
it must ask the user to send an other string.


The problem, is that once data is sent, it goes to the receive data loop 
and blocks until it gets data.





I read a little on non-blocking sockets, but i am not sure if it is what 
i should look for

I heard about timed sockets (maybe it could work, but i am unsure)

=========================================================================
import socket

remote = raw_input('To wich host:')
port = input('To wich port:')

#remote = "192.168.0.2"
#port = 22

while 1: # entering software main loop
  
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # creating 
socket
    print "\nConnecting to:",remote,":",port


    s.connect((remote, port))
    print "\nConnected to:",remote,":",port

    while 1: #Net main loop
    
        while 1: #receiving data
        
            data = s.recv(1000)
            print data
            if len(data) < 1000: break #no more data to get so break

        toSend = raw_input("Data to Send:")
        if toSend == "!QUIT" :break
        s.sendall(toSend)

        
    s.close()
    del s
        





More information about the Python-list mailing list