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处理字符串之isspace()方法的使用
May 19 Python
Windows下python2.7.8安装图文教程
May 26 Python
CentOS中升级Python版本的方法详解
Jul 10 Python
手把手教你用python抢票回家过年(代码简单)
Jan 21 Python
Python使用numpy实现BP神经网络
Mar 10 Python
Python 数据处理库 pandas 入门教程基本操作
Apr 19 Python
使用Python写一个量化股票提醒系统
Aug 22 Python
Python3.6.2调用ffmpeg的方法
Jan 10 Python
Python mutiprocessing多线程池pool操作示例
Jan 30 Python
基于python plotly交互式图表大全
Dec 07 Python
Python实现井字棋小游戏
Mar 09 Python
python使用pymongo与MongoDB基本交互操作示例
Apr 09 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 smarty的预保留变量总结
2008/12/04 PHP
Session保存到数据库的php类分享
2011/10/24 PHP
php 获取百度的热词数据的代码
2012/02/18 PHP
用来解析.htgroup文件的PHP类
2012/09/05 PHP
基于php权限分配的实现代码
2013/04/28 PHP
php发送与接收流文件的方法
2015/02/11 PHP
php基于openssl的rsa加密解密示例
2016/07/11 PHP
php redis实现文章发布系统(用户投票系统)
2017/03/04 PHP
jQuery 1.5最新版本的改进细节分析
2011/01/19 Javascript
php 中序列化和json使用介绍
2013/07/08 Javascript
window.location.href = window.location.href 跳转无反应 a超链接onclick事件写法
2013/08/21 Javascript
优化javascript的执行效率一些方法总结
2013/12/25 Javascript
jQuery实现视频作为全屏幕背景
2014/12/18 Javascript
《JavaScript DOM 编程艺术》读书笔记之JavaScript 简史
2015/01/09 Javascript
js仿苹果iwatch外观的计时器代码分享
2015/08/26 Javascript
BootStrap实现带有增删改查功能的表格(DEMO详解)
2016/10/26 Javascript
Bootstrap表单控件学习使用
2017/03/07 Javascript
node.js中express-session配置项详解
2017/05/31 Javascript
vue实现多条件和模糊搜索功能
2019/05/28 Javascript
[55:39]DOTA2-DPC中国联赛 正赛 VG vs LBZS BO3 第二场 1月19日
2021/03/11 DOTA
python网络编程示例(客户端与服务端)
2014/04/24 Python
Python中的面向对象编程详解(下)
2015/04/13 Python
Python抓取框架Scrapy爬虫入门:页面提取
2017/12/01 Python
pycharm+django创建一个搜索网页实例代码
2018/01/24 Python
让Django支持Sql Server作后端数据库的方法
2018/05/29 Python
在python中安装basemap的教程
2018/09/20 Python
解决import tensorflow导致jupyter内核死亡的问题
2021/02/06 Python
在HTML5 canvas里用卷积核进行图像处理的方法
2018/05/02 HTML / CSS
外包公司软件测试工程师
2014/11/01 面试题
如何在Shell脚本中使用函数
2015/09/06 面试题
幼师岗位求职简历的自荐信格式
2013/09/21 职场文书
政府班子四风问题整改措施
2014/10/04 职场文书
局机关干部群众路线个人对照检查材料思想汇报
2014/10/05 职场文书
2015年学校少先队工作总结
2015/07/20 职场文书
班主任远程培训研修日志
2015/11/13 职场文书
Python办公自动化之教你如何用Python将任意文件转为PDF格式
2021/06/28 Python