zoukankan      html  css  js  c++  java
  • Python2 socket 多线程并发 TCPServer Demo

    #coding=utf-8
    import socket
    import threading,getopt,sys,string
    
    opts, args = getopt.getopt(sys.argv[1:], "hp:l:",["help","port=","list="])
    #设置默认的最大连接数和端口号,在没有使用命令传入参数的时候将使用默认的值
    list=50
    port=8001
    def usage():
        print """
        -h --help             print the help
        -l --list             Maximum number of connections
        -p --port             To monitor the port number  
        """
    for op, value in opts:
        if op in ("-l","--list"):
            list = string.atol(value)
        elif op in ("-p","--port"):
            port = string.atol(value)
        elif op in ("-h"):
            usage()
            sys.exit()
    
    def jonnyS(client, address):
        try:
        #设置超时时间
            client.settimeout(30)
        #接收数据的大小
            buf = client.recv(2048)
        #将接收到的信息原样的返回到客户端中
            client.send(buf)
        #超时后显示退出
        except socket.timeout:
            print 'time out'
        #关闭与客户端的连接
        client.close()
    
    def main():
        #创建socket对象。调用socket构造函数
        #AF_INET为ip地址族,SOCK_STREAM为流套接字
        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        ip_port = ("192.168.1.103", 8093)
        #将socket绑定到指定地址,第一个参数为ip地址,第二个参数为端口号
        sock.bind(ip_port)
        #设置最多连接数量
        sock.listen(list)
        while True:
        #服务器套接字通过socket的accept方法等待客户请求一个连接
            client,address = sock.accept()
            thread = threading.Thread(target=jonnyS, args=(client, address))
            thread.start()
    
    if __name__ == '__main__':
        main()
    
  • 相关阅读:
    mybatis(2)操作数据库
    《构建之法》阅读笔记02
    mybatis(4)使用注解方式配置sql语句
    mybatis(5)参数传递
    Android Studio安装
    《构建之法》阅读笔记01
    反射
    mybatis(3)利用Mapper接口实现数据的增删改查
    activity组件
    mybatis(1)
  • 原文地址:https://www.cnblogs.com/Mysterious/p/7507221.html
Copyright © 2011-2022 走看看