博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
python2.7_1.14_编写一个简单的回显客户端/服务器应用
阅读量:6573 次
发布时间:2019-06-24

本文共 2455 字,大约阅读时间需要 8 分钟。

1、服务端 server.py

# -*- coding: utf-8 -*-import socketimport argparsehost = 'localhost'data_payload = 2048backlog = 5def echo_server(port):    # 建立一个TCP服务端    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)    # 设置套接字复用    sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)    # 绑定套接字到端口    server_address = (host, port)    print 'Starting up echo server on %s port %s' % server_address    sock.bind(server_address)    # 监听客户端,backlog值设定连接序列的最大编号    sock.listen(backlog)    while True:        print 'Waiting to receive message from client'        client, address = sock.accept()        data = client.recv(data_payload)        if data:            print 'Receive Data:%s' % data            # 返回处理过的数据            data = '
'+data+'
' client.send(data) print 'Sent %s bytes back to %s' % (data, address) # 断开连接 client.close()if __name__ == '__main__': parser = argparse.ArgumentParser(description='Socket Server Example') parser.add_argument('--port', action='store', dest='port', type=int, required=True) given_args = parser.parse_args() port = given_args.port echo_server(port)

 

2、客户端client.py

# -*- coding: utf-8 -*-import socketimport argparsehost = 'localhost'def echo_client(port):    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)    server_address = (host, port)    print 'Connecting to %s port %s' % server_address    sock.connect(server_address)    try:        message = '我是一个程序猿'        # 将内容在CMD窗口内转换成GBK编码        message = message.decode('utf-8').encode('gbk')        print 'Sending: %s' % message        sock.sendall(message)        amount_received = 0        amount_expected = len(message)        while amount_received < amount_expected:            data = sock.recv(16)            amount_received += len(data)            print 'Received:%s' % data    except socket.error, e:        print 'Socket error: %s' % str(e)    except Exception, e:        print 'Other error: %s' % str(e)    finally:        print 'Closing connection to the server'        sock.close()if __name__ == '__main__':    parser = argparse.ArgumentParser(description='Socket Server Example')    parser.add_argument('--port', action='store', dest='port', type=int, required=True)    given_args = parser.parse_args()    port = given_args.port    print port    echo_client(port)

 

3.运行

python server.py --port=9900

python client.py --port=9900

转载于:https://www.cnblogs.com/jsben/p/5165899.html

你可能感兴趣的文章
关于linux低端内存
查看>>
url 的正则表达式:path-to-regexp
查看>>
ubuntu 16.04 安装PhpMyAdmin
查看>>
安卓开启多个服务
查看>>
文件上传样式修改
查看>>
设置分录行按钮监听事件
查看>>
C Primer Plus 第5章 运算符、表达式和语句 5.2基本运算符
查看>>
蓝牙手柄按键码
查看>>
redis启动失败
查看>>
Navicat for PostgreSQL 怎么维护数据库和表
查看>>
Youtube Links
查看>>
java并发库之Executors常用的创建ExecutorService的几个方法说明
查看>>
Spring框架错误之org.springframework.beans.factory.BeanCreationException
查看>>
23种设计模式(1):单例模式
查看>>
socket 编程入门教程(五)UDP原理:4、“有连接”的UDP
查看>>
php中的三元运算符
查看>>
Introduction to CALayers Tutorial
查看>>
linux sort 命令详解
查看>>
Jquery获取iframe中的元素
查看>>
Laravel 学习笔记5.3之 Query Builder 源码解析(下)
查看>>