Needing help with sockets

Discussion in 'Python' started by Martin Dion, Jul 21, 2003.

  1. Martin Dion

    Martin Dion Guest

    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
     
    Martin Dion, Jul 21, 2003
    #1
    1. Advertising

  2. Martin Dion wrote:

    > Thank you for your reply, but I have not been able to find good information
    > about the select module for a client. Do you know a good place to find
    > tutorials about select module for clients ?


    I'm not sure what you mean with 'select module for a client'...
    What do you need to know that is not in the module documentation
    or the Python socket howto?

    Anyway have a look at the following mini example that I whipped up:

    -------------
    from select import select
    from socket import *
    import sys

    sock=socket(AF_INET, SOCK_STREAM)
    sock.connect( ('ftp.xs4all.nl', 21) ) # ftp port

    print '>', # prompt

    while 1:
    ins, outs, exs = select([sys.stdin, sock], [], [])
    if sys.stdin in ins:
    userline=sys.stdin.readline()
    sock.send(userline)
    print '>',
    if sock in ins:
    socketdata=sock.recv(1000) # buffer = 1000
    if socketdata:
    print socketdata,
    else:
    break # no more data

    -------------

    Does this help? If not, please describe in more detail what you
    want to achive, what you've already tried and why that didn't work.

    Good luck!

    --Irmen de Jong
     
    Irmen de Jong, Jul 24, 2003
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ravikanth[MVP]

    Needing Help with a webpage

    Ravikanth[MVP], Aug 4, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    289
    Ravikanth[MVP]
    Aug 4, 2003
  2. Kevin Spencer

    Re: Needing Help with a webpage

    Kevin Spencer, Aug 4, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    406
    Kevin Spencer
    Aug 4, 2003
  3. Replies:
    7
    Views:
    526
  4. raecer

    newbie needing help

    raecer, Nov 19, 2004, in forum: C++
    Replies:
    3
    Views:
    328
    Phlip
    Nov 19, 2004
  5. Replies:
    4
    Views:
    342
    Jeff Dillon
    May 17, 2006
Loading...

Share This Page