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编程中的字符串
Oct 14 Python
python开发之IDEL(Python GUI)的使用方法图文详解
Nov 12 Python
Phantomjs抓取渲染JS后的网页(Python代码)
May 13 Python
Python实现复杂对象转JSON的方法示例
Jun 22 Python
Python爬取附近餐馆信息代码示例
Dec 09 Python
Python向Excel中插入图片的简单实现方法
Apr 24 Python
解决matplotlib库show()方法不显示图片的问题
May 24 Python
Python使用matplotlib实现基础绘图功能示例
Jul 03 Python
python 非线性规划方式(scipy.optimize.minimize)
Feb 11 Python
如何用python实现一个HTTP连接池
Jan 14 Python
selenium+python实现基本自动化测试的示例代码
Jan 27 Python
python中的getter与setter你了解吗
Mar 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
php中使用Curl、socket、file_get_contents三种方法POST提交数据
2011/08/12 PHP
PHP获取数组中重复最多的元素的实现方法
2014/11/11 PHP
分享3个php获取日历的函数
2015/09/25 PHP
php自动载入类用法实例分析
2016/06/24 PHP
手把手编写PHP框架 深入了解MVC运行流程
2016/09/19 PHP
PHP+Mysql无刷新问答评论系统(源码)
2016/12/20 PHP
传智播客学习之JavaScript基础篇
2009/11/13 Javascript
基于jquery的inputlimiter 实现字数限制功能
2010/05/30 Javascript
手机号码,密码正则验证
2014/09/04 Javascript
Javascript数据结构与算法之列表详解
2015/03/12 Javascript
基于JavaScript获取鼠标位置的各种方法
2015/12/16 Javascript
jQuery form 表单验证插件(fieldValue)校验表单
2016/01/24 Javascript
Angular 中 select指令用法详解
2016/09/29 Javascript
详解Angularjs 如何自定义Img的ng-load 事件
2017/02/15 Javascript
js从输入框读取内容,比较两个数字的大小方法
2017/03/13 Javascript
ES6中Iterator与for..of..遍历用法分析
2017/03/31 Javascript
JavaScript实现的反序列化json字符串操作示例
2018/07/18 Javascript
vue 之 css module的使用方法
2018/12/04 Javascript
JavaScript常用工具方法封装
2019/02/12 Javascript
关于angular引入ng-zorro的问题浅析
2020/09/09 Javascript
Python中使用摄像头实现简单的延时摄影技术
2015/03/27 Python
python实现批量改文件名称的方法
2015/05/25 Python
python编程开发之日期操作实例分析
2015/11/13 Python
Python实现的多线程http压力测试代码
2017/02/08 Python
用python统计代码行的示例(包括空行和注释)
2018/07/24 Python
Django的models模型的具体使用
2019/07/15 Python
python通过txt文件批量安装依赖包的实现步骤
2019/08/13 Python
Pytorch使用MNIST数据集实现基础GAN和DCGAN详解
2020/01/10 Python
css3 中实现炫酷的loading效果
2019/04/26 HTML / CSS
英国男士时尚购物网站:Stuarts London
2017/10/22 全球购物
芝加哥牛排公司:Chicago Steak Company
2018/10/31 全球购物
建筑文秘专业个人求职信范文
2013/12/28 职场文书
培训心得体会
2013/12/29 职场文书
给校长的建议书作文400字
2015/09/14 职场文书
Python实现文本文件拆分写入到多个文本文件的方法
2021/04/18 Python
Go语言实现Base64、Base58编码与解码
2021/07/26 Golang