How to use python socket.settimeout() properly

前端 未结 3 1066
自闭症患者
自闭症患者 2020-12-31 13:33

As far as I know, when you call socket.settimeout(value) and you set a float value greater than 0.0, that socket will raise a scocket.timeout when a call to, fo

3条回答
  •  南方客
    南方客 (楼主)
    2020-12-31 14:13

    See my server script, you will get the idea to use it properly.

    import socket
    import sys
    fragments = []
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.bind(("192.168.1.4",9001))
    s.listen(5)
    while True:
        c,a = s.accept()
        c.settimeout(10.0)
        print "Someone came in Server from %s and port %s" %(a[0],a[1])
        c.send("Welcome to system")
        while True:
            chunk = c.recv(2048)
            if not chunk.strip():
                break
            else:
                fragments.append(chunk)
                continue
        combiner = "".join(fragments)
        print combiner
        shutdown = str(raw_input("Wanna Quit(Y/y) or (N/n): "))
        if shutdown == 'Y' or shutdown == 'y':
            c.close()
            sys.exit()
        else:
            continue
    

    This script is just to give you an idea about the socket.settimeout().

提交回复
热议问题