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 相关文章推荐
下载给定网页上图片的方法
Feb 18 Python
Python找出list中最常出现元素的方法
Jun 14 Python
python之virtualenv的简单使用方法(必看篇)
Nov 25 Python
解决pandas无法在pycharm中使用plot()方法显示图像的问题
May 24 Python
python 调用钉钉机器人的方法
Feb 20 Python
python文字转语音的实例代码分析
Nov 12 Python
关于Tensorflow使用CPU报错的解决方式
Feb 05 Python
TensorFlow实现模型断点训练,checkpoint模型载入方式
May 26 Python
django 实现后台从富文本提取纯文本
Jul 02 Python
通过案例解析python鸭子类型相关原理
Oct 10 Python
python基于selenium爬取斗鱼弹幕
Feb 20 Python
Python实现Telnet自动连接检测密码的示例
Apr 16 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
非洲第一个咖啡超凡杯大赛承办国—卢旺达的咖啡怎么样
2021/03/03 咖啡文化
使用 MySQL Date/Time 类型
2008/03/26 PHP
php实现cc攻击防御和防止快速刷新页面示例
2014/02/13 PHP
php计算2个日期的差值函数分享
2015/02/02 PHP
详解PHP中cookie和session的区别及cookie和session用法小结
2016/06/12 PHP
Yii2创建多界面主题(Theme)的方法
2016/10/08 PHP
php基于websocket搭建简易聊天室实践
2016/10/24 PHP
PHP实现支持CURL字符串证书传输的方法
2019/03/23 PHP
用prototype实现的简单小巧的多级联动菜单
2007/03/24 Javascript
Google Map Api和GOOGLE Search Api整合实现代码
2009/07/18 Javascript
js jquery数组介绍
2012/07/15 Javascript
javascript函数特点实例分析
2015/05/14 Javascript
在Node.js中使用HTTP上传文件的方法
2015/06/23 Javascript
AngularJS 模型详细介绍及实例代码
2016/07/27 Javascript
jQuery插件HighCharts绘制2D饼图效果示例【附demo源码下载】
2017/03/21 jQuery
微信小程序列表渲染功能之列表下拉刷新及上拉加载的实现方法分析
2017/11/27 Javascript
Node.js中console.log()输出彩色字体的方法示例
2019/12/01 Javascript
vue excel上传预览和table内容下载到excel文件中
2019/12/10 Javascript
maptalks+three.js+vue webpack实现二维地图上贴三维模型操作
2020/08/10 Javascript
Python EOL while scanning string literal问题解决方法
2020/09/18 Python
使用python中的in ,not in来检查元素是不是在列表中的方法
2018/07/06 Python
python+os根据文件名自动生成文本
2019/03/21 Python
详解Python 重学requests发起请求的基本方式
2020/02/07 Python
CSS3属性 line-clamp控制文本行数的使用
2020/03/19 HTML / CSS
C/C++有关内存的思考题
2015/12/04 面试题
四年大学生活的个人自我评价
2013/12/11 职场文书
医学专业本科毕业生自我鉴定
2013/12/28 职场文书
优秀毕业自我鉴定
2014/02/15 职场文书
洗发露广告词
2014/03/14 职场文书
2014年母亲节寄语
2014/05/07 职场文书
学习雷锋精神演讲稿
2014/05/10 职场文书
运动会拉拉队口号
2014/06/09 职场文书
教师四风自我剖析材料
2014/09/30 职场文书
2015年初一班主任工作总结
2015/05/13 职场文书
vue实现无缝轮播效果(跑马灯)
2021/05/14 Vue.js
golang实现一个简单的websocket聊天室功能
2021/10/05 Golang