• python网络编程


    #server.py

    from socket import *
    from time import ctime

    HOST = ''
    PORT = 21567
    BUFSIZ = 1024
    ADDR = (HOST, PORT)

    tcpSerSock = socket(AF_INET, SOCK_STREAM)
    tcpSerSock.bind(ADDR)
    tcpSerSock.listen(5)

    while True:
    print 'waiting for connecting'
    tcpCliSock, addr = tcpSerSock.accept()
    print 'connected from:', addr
    print tcpCliSock
    while True:
    data = tcpCliSock.recv(BUFSIZ)
    if not data:
    break
    tcpCliSock.send('[%s] %s' % (ctime(), data))
    tcpCliSock.close()
    tcpSerSock.close()


    #client.py
    from socket import *
    HOST = '127.0.0.1'
    PORT = 21567
    BUFSIZ = 1024
    ADDR = (HOST, PORT)

    tcpCliSock = socket(AF_INET, SOCK_STREAM)
    tcpCliSock.connect(ADDR)
    while True:
    data = raw_input('> ')
    if not data:
    break
    tcpCliSock.send(data)
    data = tcpCliSock.recv(BUFSIZ)
    if not data:
    break
    print data
    tcpCliSock.close()




    #socketserver
    from SocketServer import TCPServer as TCP, StreamRequestHandler as SRH
    from time import ctime

    HOST = ''
    PORT = 21567
    ADDR = (HOST, PORT)

    class MyRequestHandler(SRH):
    def handle(self):
    print 'connected from:', self.client_address
    self.wfile.write('[%s] %s' % (ctime(), self.rfile.readline()))
    tcpServ = TCP(ADDR, MyRequestHandler)
    print 'waiting for connecting...'
    tcpServ.serve_forever()

    #socketclient
    from socket import *
    HOST = '127.0.0.1'
    PORT = 21567
    BUFSIZ = 1024
    ADDR = (HOST, PORT)

    while True:
    tcpCliSock = socket(AF_INET, SOCK_STREAM)
    tcpCliSock.connect(ADDR)
    data = raw_input('> ')
    if not data:
    break
    tcpCliSock.send('%s ' % data)
    data = tcpCliSock.recv(BUFSIZ)
    if not data:
    break
    print data.strip()
    tcpCliSock.close()


    
    
  • 相关阅读:
    使用IDENTITY列属性和Sequence对象
    使用OFFSET-FETCH进行数据过滤
    SQL 插入语句汇总
    建立&修改视图
    Centos 7.x 搭建 Zabbix3.4
    RDS 导出Mysqlbinlog_二进制日志
    Yac
    云服务器漏洞更新
    Centos 内存释放
    Centos 安装 Htop
  • 原文地址:https://www.cnblogs.com/mylovelulu/p/9294905.html
Copyright © 2020-2023  润新知