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生成随机mac地址的方法
Mar 16 Python
Python 处理数据的实例详解
Aug 10 Python
pycharm修改界面主题颜色的方法
Jan 17 Python
Python的UTC时间转换讲解
Feb 26 Python
Python下opencv图像阈值处理的使用笔记
Aug 04 Python
Python 转换文本编码实现解析
Aug 27 Python
Python3 把一个列表按指定数目分成多个列表的方式
Dec 25 Python
python opencv实现信用卡的数字识别
Jan 12 Python
python实现在内存中读写str和二进制数据代码
Apr 24 Python
Python爬虫爬取微信朋友圈
Aug 06 Python
python爬不同图片分别保存在不同文件夹中的实现
Apr 02 Python
python数据分析之用sklearn预测糖尿病
Apr 22 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的password_hash()使用实例
2014/03/17 PHP
在Thinkphp中使用ajax实现无刷新分页的方法
2016/10/25 PHP
在标题栏显示新消息提示,很多公司项目中用到这个方法
2011/11/04 Javascript
文本框获得焦点和失去焦点的判断代码
2012/03/18 Javascript
jquery的live使用注意事项
2014/02/18 Javascript
JavaScript字符串对象substring方法入门实例(用于截取字符串)
2014/10/17 Javascript
Perl Substr()函数及函数的应用
2015/12/16 Javascript
javascript中的作用域和闭包详解
2016/01/13 Javascript
深入理解MVC中的时间js格式化
2016/05/19 Javascript
jquery实现拖动效果
2016/08/10 Javascript
JS 获取HTML标签内的子节点的方法
2016/09/21 Javascript
JavaScript ES6中CLASS的使用详解
2016/11/22 Javascript
Google 爬虫如何抓取 JavaScript 的内容
2017/04/07 Javascript
微信小程序开发之从相册获取图片 使用相机拍照 本地图片上传
2017/04/18 Javascript
JavaScript实现单击网页任意位置打开新窗口与关闭窗口的方法
2017/09/21 Javascript
node 利用进程通信实现Cluster共享内存
2017/10/27 Javascript
vue内置组件transition简单原理图文详解(小结)
2018/07/12 Javascript
vue-image-crop基于Vue的移动端图片裁剪组件示例
2018/08/28 Javascript
Angular使用Restful的增删改
2018/12/28 Javascript
详解Vue 数据更新了但页面没有更新的 7 种情况汇总及延伸总结
2020/05/28 Javascript
python爬虫教程之爬取百度贴吧并下载的示例
2014/03/07 Python
python中的格式化输出用法总结
2016/07/28 Python
Python 中的with关键字使用详解
2016/09/11 Python
Python 字符串转换为整形和浮点类型的方法
2018/07/17 Python
python时间序列按频率生成日期的方法
2019/05/14 Python
美国受信赖的教育产品供应商:Nest Learning
2018/06/14 全球购物
Nike加拿大官网:Nike.com (CA)
2019/04/09 全球购物
Street One瑞士:德国现代时装公司
2019/10/09 全球购物
什么是测试驱动开发(TDD)
2012/02/15 面试题
美术教师自我鉴定
2014/02/12 职场文书
财务务虚会发言材料
2014/10/20 职场文书
公务员个人年终总结
2015/02/12 职场文书
党校个人总结
2015/03/04 职场文书
2019事业单位个人工作总结范文
2019/08/26 职场文书
修改MySQL的数据库引擎为INNODB的方法
2021/05/26 MySQL
Python爬取用户观影数据并分析用户与电影之间的隐藏信息!
2021/06/29 Python