zoukankan      html  css  js  c++  java
  • 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()


    
    
  • 相关阅读:
    IntelliJ IDEA 偏好设置
    Unix环境下的5中IO模型
    Hbase原理、基本概念、基本架构
    可参考的技术博客
    Hadoop生态系统介绍
    oracle 添加表分区和索引分区,修改索引分区默认表空间
    koa redis 链接
    Node-APN 开源推送服务
    NodeJs 笔记
    windows防火墙无法启动,服务不存在
  • 原文地址:https://www.cnblogs.com/mylovelulu/p/9294905.html
Copyright © 2011-2022 走看看