• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell
您的位置:首页 > 脚本语言 >python > python实现TCP服务器端与客户端的方法详解

python实现TCP服务器端与客户端的方法详解

作者: 字体:[增加 减小] 来源:互联网

通过本文主要向大家介绍了python tcp 服务器,python tcpserver,python socket tcp,python tcp,python tcp/ip等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

本文实例讲述了python实现TCP服务器端与客户端的方法。分享给大家供大家参考。具体如下:

TCP服务器程序(tsTserv.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 connection...'
  tcpCliSock, addr = tcpSerSock.accept()
  print '...connected from:', addr
  while True:
    data = tcpCliSock.recv(BUFSIZ)
    if not data:
      break
    tcpCliSock.send('[%s] %s' %(ctime(), data))
  tcpCliSock.close()
tcpSerSock.close()
</div>

TCP客户端程序(tsTclnt.py):

from socket import *
HOST = 'localhost'
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)
  data1 = tcpCliSock.recv(BUFSIZ)
  if not data1:
    break
  print data1
tcpCliSock.close()
</div>

运行说明:先运行服务器程序,作用类似于打开服务器保持等待客户请求,再运行客户端程序。

运行界面如下:

服务器端:

D:\code\ex>python tsTserv.py
waiting for connection...
...connected from: ('127.0.0.1', 2883)
waiting for connection...
...connected from: ('127.0.0.1', 2885)
waiting for connection...
...connected from: ('127.0.0.1', 2889)
waiting for connection...
...connected from: ('127.0.0.1', 2891)
waiting for connection...
...connected from: ('127.0.0.1', 2892)
waiting for connection...
...connected from: ('127.0.0.1', 2893)
waiting for connection...
</div>

客户端:

D:\code\ex>python tsTclnt.py
> 1
[Thu Feb 02 15:52:21 2012] 1
> 2
[Thu Feb 02 15:52:22 2012] 2
> 3
[Thu Feb 02 15:52:22 2012] 3
> 5
[Thu Feb 02 15:52:23 2012] 5
> 6
[Thu Feb 02 15:52:24 2012] 6
>
D:\code\ex>
</div>

希望本文所述对大家的Python程序设计有所帮助。

</div>

您可能想查找下面的文章:

  • python实现TCP服务器端与客户端的方法详解
  • python实现TCP服务器端与客户端的方法详解
  • python检测远程服务器tcp端口的方法
  • python检测远程服务器tcp端口的方法
  • python实现简单的TCP代理服务器
  • python实现简单的TCP代理服务器

相关文章

  • 分析并输出Python代码依赖的库的实现代码
  • python中pandas.DataFrame的简单操作方法(创建、索引、增添与删除)
  • Python中dictionary items()系列函数的用法实例
  • Python实现Linux命令xxd -i功能
  • Python RuntimeError: thread.__init__() not called解决方法
  • Python编程语言的35个与众不同之处(语言特征和使用技巧)
  • 对于Python编程中一些重用与缩减的建议
  • python中常用的各种数据库操作模块和连接实例
  • Python pickle类库介绍(对象序列化和反序列化)
  • pyqt和pyside开发图形化界面

文章分类

  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell

最近更新的内容

    • python中getaddrinfo()基本用法实例分析
    • 使用grappelli为django admin后台添加模板
    • python实现中文转换url编码的方法
    • python使用正则表达式提取网页URL的方法
    • 详解python 发送邮件实例代码
    • python获取指定目录下所有文件名列表的方法
    • Python字符串替换实例分析
    • python动态监控日志内容的示例
    • python 读写、创建 文件的方法(必看)
    • python 捕获shell脚本的输出结果实例

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有