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中使用sort()方法进行排序的简单教程
May 21 Python
Python的包管理器pip更换软件源的方法详解
Jun 20 Python
python监控linux内存并写入mongodb(推荐)
Sep 11 Python
详解如何利用Cython为Python代码加速
Jan 27 Python
对numpy的array和python中自带的list之间相互转化详解
Apr 13 Python
在linux下实现 python 监控usb设备信号
Jul 03 Python
python实现邮件发送功能
Aug 10 Python
python-Web-flask-视图内容和模板知识点西宁街
Aug 23 Python
python同义词替换的实现(jieba分词)
Jan 21 Python
使用pymysql查询数据库,把结果保存为列表并获取指定元素下标实例
May 15 Python
python实现人性化显示金额数字实例详解
Sep 25 Python
Django debug为True时,css加载失败的解决方案
Apr 24 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
自己动手做一个SQL解释器
2006/10/09 PHP
PHP日期处理函数 整型日期格式
2011/01/12 PHP
php实现快速排序的三种方法分享
2014/03/12 PHP
Laravel4中的Validator验证扩展用法详解
2016/07/26 PHP
php 输入输出流详解及示例代码
2016/08/25 PHP
javascript实现的动态文字变换
2007/07/28 Javascript
解决用jquery load加载页面到div时,不执行页面js的问题
2014/02/22 Javascript
jQuery定义背景动态切换效果的方法
2015/03/23 Javascript
用JavaScript实现PHP的urlencode与urldecode函数
2015/08/13 Javascript
学习JavaScript设计模式之观察者模式
2020/04/22 Javascript
深入浅析JavaScript中的constructor
2016/04/19 Javascript
Bootstrap和Angularjs配合自制弹框的实例代码
2016/08/24 Javascript
利用jquery给指定的table动态添加一行、删除一行的方法
2016/10/12 Javascript
jquery select2的使用心得(推荐)
2016/12/04 Javascript
ajax实现动态下拉框示例
2017/01/10 Javascript
jQuery插件select2利用ajax高效查询大数据列表(可搜索、可分页)
2017/05/19 jQuery
BootStrap表单控件之文本域textarea
2017/05/23 Javascript
详解vue中点击空白处隐藏div的实现(用指令实现)
2018/04/19 Javascript
Mac下通过brew安装指定版本的nodejs教程
2018/05/17 NodeJs
js中offset,client , scroll 三大元素知识点总结
2019/09/11 Javascript
深入学习Vue nextTick的用法及原理
2019/10/08 Javascript
ES6 Symbol在对象中的作用实例分析
2020/06/06 Javascript
python目录操作之python遍历文件夹后将结果存储为xml
2014/01/27 Python
玩转python爬虫之爬取糗事百科段子
2016/02/17 Python
Python过滤列表用法实例分析
2016/04/29 Python
Python读取YAML文件过程详解
2019/12/30 Python
Python基于yaml文件配置logging日志过程解析
2020/06/23 Python
OpenCV+Python3.5 简易手势识别的实现
2020/12/21 Python
浅析图片上传及canvas压缩的流程
2020/06/10 HTML / CSS
在线购买廉价折扣书籍和小说:BookOutlet.com
2018/02/19 全球购物
编码实现字符串转整型的函数
2012/06/02 面试题
毕业生大学生活自我总结
2014/01/31 职场文书
《老山界》教学反思
2014/04/08 职场文书
大三学生英语考试作弊检讨书
2015/01/01 职场文书
超级实用的公文标题大全!
2019/07/19 职场文书
Python中Selenium对Cookie的操作方法
2021/07/09 Python