Python简单网络编程示例【客户端与服务端】


Posted in Python onMay 26, 2017

本文实例讲述了Python简单网络编程。分享给大家供大家参考,具体如下:

内容目录

1. 客户端(client.py)
2. 服务端(server.py)

一、客户端(client.py)

import socket
import sys
port = 70
host = sys.argv[1]
filename = sys.argv[2]
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((host, port))
fd = s.makefile("rw", 0)
fd.write(filename + "\n")
for line in fd.readlines():
  sys.stdout.write(line)

程序通过socket.socket()建立一个Socket,参数告诉系统需要一个Internet Socket进行TCP通信。接着程序链接远程的主机名,并提供文件名。最后获得响应后在屏幕上打印出来。

测试

python client.py quux.org /

显示

iWelcome to gopher at quux.org! fake  (NULL) 0
i  fake  (NULL) 0
iThis server has a lot of information of historic interest, fake  (NULL) 0
ifunny, or just plain entertaining -- all presented in Gopher. fake  (NULL) 0
iThere are many mirrors here of rare or valuable files with the fake  (NULL) 0
iaim to preserve them in case their host disappears. PLEASE READ  fake  (NULL) 0
i"About This Server" FOR IMPORTANT NOTES AND LEGAL INFORMATION. fake  (NULL) 0
i  fake  (NULL) 0
0About This Server /About This Server.txt gopher.quux.org 70 +
1Archives  /Archives  gopher.quux.org 70 +
1Books /Books gopher.quux.org 70 +
1Communication /Communication gopher.quux.org 70 +
iThis directory contains the entire text of the book  fake  (NULL) 0
i"We the Media: Grassroots Journalism by the People, for the People"  fake  (NULL) 0
iby Dan Gillmor in various formats. fake  (NULL) 0
i  fake  (NULL) 0
iFeel free to download and enjoy.  fake  (NULL) 0
1Computers /Computers gopher.quux.org 70 +
1Current Issues and Events (Updated Apr. 23, 2002) /Current  gopher.quux.org 70 +
1Development Projects  /devel gopher.quux.org 70 +
0Gopher's 10th Anniversary /3.0.0.txt gopher.quux.org 70
1Government, Politics, Law, and Conflict  /Government gopher.quux.org 70 +
0How To Help  /How To Help.txt  gopher.quux.org 70 +
1Humor and Fun /Humor and Fun gopher.quux.org 70 +
1Index to Quux.Org /Archives/index gopher.quux.org 70
1Internet  /Internet  gopher.quux.org 70 +
1Other Gopher Servers  /Software/Gopher/servers  gopher.quux.org 70
1People /People gopher.quux.org 70 +
1Reference /Reference gopher.quux.org 70 +
1Software and Downloads /Software  gopher.quux.org 70 +
1The Gopher Project /Software/Gopher  gopher.quux.org 70
0What's New /whatsnew.txt  gopher.quux.org 70 + 

二、服务端(server.py)

# coding: utf-8
import socket
host = ''
port = 51421
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind((host, port))
s.listen(1)        #每次最多只有一个等候处理
print "Server is running on port %d; press Ctrl-C to terminate." %port
while 1:
  clientsock, clientaddr = s.accept()
  clientfile = clientsock.makefile('rw', 0)
  clientfile.write("Welcome, " + str(clientaddr) + "\n")
  clientfile.write("Please enter a string: ")
  line = clientfile.readline().strip()
  clientfile.write("You entered %d characters. \n" %len(line))
  clientfile.close()
  clientsock.close()

建立一个socket,设置成可复用的(reusable),绑定端口号51421(可选大于1024的任一值),调用listen()函数,开始等待来自客户端的请求,同时设定最多只有一个等候处理的链接。

主循环对a.accept()函数调用开始,程序连接一个客户端后立马停止,接收用户的输入。

运行一个例子

首先运行server.py

python server.py

另开一个终端,连接localhost的51421端口。 

jihite@ubuntu:~/web$ telnet localhost 51421
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
Welcome, ('127.0.0.1', 59853)
Please enter a string: mm
You entered 2 characters.
Connection closed by foreign host.

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

Python 相关文章推荐
使用python解析xml成对应的html示例分享
Apr 02 Python
python调用windows api锁定计算机示例
Apr 17 Python
python函数装饰器用法实例详解
Jun 04 Python
python使用opencv按一定间隔截取视频帧
Mar 06 Python
用python写扫雷游戏实例代码分享
May 27 Python
详解python实现交叉验证法与留出法
Jul 11 Python
Django项目使用CircleCI的方法示例
Jul 14 Python
python中栈的原理及实现方法示例
Nov 27 Python
Python模块_PyLibTiff读取tif文件的实例
Jan 13 Python
python操作微信自动发消息的实现(微信聊天机器人)
Jul 14 Python
浅谈Python xlwings 读取Excel文件的正确姿势
Feb 26 Python
浅析Python OpenCV三种滤镜效果
Apr 11 Python
Python编程对列表中字典元素进行排序的方法详解
May 26 #Python
利用Python实现网络测试的脚本分享
May 26 #Python
python 如何快速找出两个电子表中数据的差异
May 26 #Python
详解Python3操作Mongodb简明易懂教程
May 25 #Python
python爬虫入门教程--正则表达式完全指南(五)
May 25 #Python
python爬虫入门教程--HTML文本的解析库BeautifulSoup(四)
May 25 #Python
Python win32com 操作Exce的l简单方法(必看)
May 25 #Python
You might like
DC《神奇女侠2》因疫情推迟上映 温子仁新恐怖片《恶性》撤档
2020/04/09 欧美动漫
PHP与SQL注入攻击[三]
2007/04/17 PHP
深入解析php中的foreach问题
2013/06/30 PHP
PHP中使用addslashes函数转义的安全性原理分析
2014/11/03 PHP
修改yii2.0用户登录使用的user表为其它的表实现方法(推荐)
2017/08/01 PHP
thinkphp5框架API token身份验证功能示例
2019/05/21 PHP
Yii2框架配置文件(Application属性)与调试技巧实例分析
2019/05/27 PHP
PHP如何将图片文件上传到另外一台服务器上
2019/08/26 PHP
本地对象Array的原型扩展实现代码
2010/12/04 Javascript
使用Grunt.js管理你项目的应用说明
2013/04/24 Javascript
JQ获取动态加载的图片大小的正确方法分享
2013/11/08 Javascript
js 页面元素的几个用法总结
2013/11/18 Javascript
javascript进行数组追加方法小结
2014/06/16 Javascript
JavaScript实现点击单选按钮改变输入框中文本域内容的方法
2015/08/12 Javascript
js随机生成26个大小写字母
2016/02/12 Javascript
解析JavaScript面向对象概念中的Object类型与作用域
2016/05/10 Javascript
JavaScript数据结构之二叉查找树的定义与表示方法
2017/04/12 Javascript
AngularJS的脏检查深入分析
2017/04/22 Javascript
Node.js 8 中的重要新特性
2017/06/28 Javascript
基于vue2.x的电商图片放大镜插件的使用
2018/01/22 Javascript
Python对列表排序的方法实例分析
2015/05/16 Python
在Python程序中操作文件之isatty()方法的使用教程
2015/05/24 Python
在Python中的Django框架中进行字符串翻译
2015/07/27 Python
Django 开发环境配置过程详解
2019/07/18 Python
python统计字符的个数代码实例
2020/02/07 Python
如何使用canvas绘制可移动网格的示例代码
2020/12/14 HTML / CSS
魅力惠奢品线上平台:MEI.COM
2016/11/29 全球购物
学前教育毕业生自荐信
2013/10/29 职场文书
现金会计岗位职责
2013/12/05 职场文书
给老师的道歉信
2014/01/11 职场文书
家长会主持词开场白
2014/03/18 职场文书
保护环境建议书400字
2014/05/13 职场文书
青春励志演讲稿范文
2014/08/25 职场文书
教育项目合作协议书格式
2014/10/17 职场文书
详解Python小数据池和代码块缓存机制
2021/04/07 Python
探究Mysql模糊查询是否区分大小写
2021/06/11 MySQL