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中操作MySQL入门实例
Feb 08 Python
Python中使用Inotify监控文件实例
Feb 14 Python
用Python制作简单的钢琴程序的教程
Apr 01 Python
python实现带声音的摩斯码翻译实现方法
May 20 Python
python下paramiko模块实现ssh连接登录Linux服务器
Jun 03 Python
Django的数据模型访问多对多键值的方法
Jul 21 Python
Python编程修改MP3文件名称的方法
Apr 19 Python
Python进阶-函数默认参数(详解)
May 18 Python
Python常用字符串替换函数strip、replace及sub用法示例
May 21 Python
用Python实现数据的透视表的方法
Nov 16 Python
Python TestCase中的断言方法介绍
May 02 Python
python3文件复制、延迟文件复制任务的实现方法
Sep 02 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
php文档更新介绍
2011/07/22 PHP
mysql总结之explain
2012/02/27 PHP
PHP mysql与mysqli事务使用说明 分享
2013/08/17 PHP
设置php页面编码的两种方法示例介绍
2014/03/03 PHP
php 删除cookie方法详解
2014/12/01 PHP
PHP实现从上往下打印二叉树的方法
2018/01/18 PHP
详解laravel安装使用Passport(Api认证)
2018/07/27 PHP
javascript 读取图片文件的大小
2009/06/25 Javascript
Iframe 自适应高度并实时监控高度变化的js代码
2009/10/30 Javascript
jQuery toggle()设置CSS样式
2009/11/05 Javascript
javascript中创建对象的三种常用方法
2010/12/30 Javascript
javascript中的对象创建 实例附注释
2011/02/08 Javascript
javascript轻量级模板引擎juicer使用指南
2014/06/22 Javascript
jQuery实现瀑布流的取巧做法分享
2015/01/12 Javascript
学习javascript面向对象 实例讲解面向对象选项卡
2016/01/04 Javascript
微信小程序的动画效果详解
2017/01/18 Javascript
JavaScript实现简单的星星评分效果
2017/05/18 Javascript
vue form check 表单验证的实现代码
2018/12/09 Javascript
Vue中Axios从远程/后台读取数据
2019/01/21 Javascript
如何让微信小程序页面之间的通信不再变困难
2019/06/03 Javascript
webpack + vue 打包生成公共配置文件(域名) 方便动态修改
2019/08/29 Javascript
JavaScript实现简单计算器功能
2019/12/19 Javascript
[00:35]TI7不朽珍藏III——寒冰飞龙不朽展示
2017/07/15 DOTA
python3使用urllib示例取googletranslate(谷歌翻译)
2014/01/23 Python
python实现读取excel写入mysql的小工具详解
2017/11/20 Python
Python入门学习指南分享
2018/04/11 Python
python3.6.3+opencv3.3.0实现动态人脸捕获
2018/05/25 Python
Python3中函数参数传递方式实例详解
2019/05/05 Python
PyQt5的安装配置过程,将ui文件转为py文件后显示窗口的实例
2019/06/19 Python
python3中的eval和exec的区别与联系
2019/10/10 Python
使用matplotlib绘制图例标签中带有公式的图
2019/12/13 Python
Python 中如何使用 virtualenv 管理虚拟环境
2021/01/21 Python
个人查摆问题整改措施
2014/10/04 职场文书
React列表栏及购物车组件使用详解
2021/06/28 Javascript
Mysql数据库中datetime、bigint、timestamp来表示时间选择,谁来存储时间效率最高
2021/08/23 MySQL
MySQL表字段数量限制及行大小限制详情
2022/07/23 MySQL