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调用cmd复制文件代码分享
Dec 27 Python
python实现在sqlite动态创建表的方法
May 08 Python
Python文件及目录操作实例详解
Jun 04 Python
带你了解python装饰器
Jun 15 Python
python与caffe改变通道顺序的方法
Aug 04 Python
python 多线程将大文件分开下载后在合并的实例
Nov 09 Python
Python实现判断一个整数是否为回文数算法示例
Mar 02 Python
Pytorch之finetune使用详解
Jan 18 Python
Python模块/包/库安装的六种方法及区别
Feb 24 Python
python opencv 图像边框(填充)添加及图像混合的实现方法(末尾实现类似幻灯片渐变的效果)
Mar 09 Python
python基于win32api实现键盘输入
Dec 09 Python
jupyter notebook更换皮肤主题的实现
Jan 07 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中this,self,parent的区别详解
2013/06/08 PHP
phpstudy2020搭建站点的实现示例
2020/10/30 PHP
js 创建快捷方式的代码(fso)
2010/11/19 Javascript
IE6 fixed的完美解决方案
2011/03/31 Javascript
Jquery节点遍历next与nextAll方法使用示例
2014/07/22 Javascript
jquery实现漫天雪花飞舞的圣诞祝福雪花效果代码分享
2015/08/20 Javascript
jquery获取select选中值的方法分析
2015/12/22 Javascript
canvas 绘制圆形时钟
2017/02/22 Javascript
使用ionic播放轮询广告的实现方法(必看)
2017/04/24 Javascript
Vue应用部署到服务器的正确方式
2017/07/15 Javascript
微信小程序实现根据字母选择城市功能
2017/08/16 Javascript
JS随机排序数组实现方法分析
2017/10/11 Javascript
angularJS自定义directive之带参方法传递详解
2018/10/09 Javascript
react写一个select组件的实现代码
2019/04/03 Javascript
vue+element tabs选项卡分页效果
2020/06/29 Javascript
使用webpack搭建pixi.js开发环境
2020/02/12 Javascript
vue@cli3项目模板怎么使用public目录下的静态文件
2020/07/07 Javascript
three.js 将图片马赛克化的示例代码
2020/07/31 Javascript
JS实现悬浮球只在一侧滑动并且是横屏状态下
2020/08/19 Javascript
Python内置数据类型详解
2014/08/18 Python
线程和进程的区别及Python代码实例
2015/02/04 Python
python使用datetime模块计算各种时间间隔的方法
2015/03/24 Python
python使用sorted函数对列表进行排序的方法
2015/04/04 Python
python实现颜色空间转换程序(Tkinter)
2015/12/31 Python
python opencv 批量改变图片的尺寸大小的方法
2019/06/28 Python
python 处理微信对账单数据的实例代码
2019/07/19 Python
pygame编写音乐播放器的实现代码示例
2019/11/19 Python
Python面向对象程序设计之静态方法、类方法、属性方法原理与用法分析
2020/03/23 Python
python 实现简单的计算器(gui界面)
2020/11/11 Python
德国低价购买灯具和家具网站:Style-home.de
2016/11/25 全球购物
Lookfantastic日本官网:英国知名护肤、化妆品和头发护理购物网站
2018/04/21 全球购物
印度尼西亚电子产品购物网站:Kliknklik
2018/06/05 全球购物
简单说说tomcat的配置
2013/05/28 面试题
森林病虫害防治方案
2014/06/02 职场文书
品质口号大全
2014/06/17 职场文书
html中两种获取标签内的值的方法
2022/06/10 HTML / CSS