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实现查询苹果手机维修进度
Mar 16 Python
Python三级目录展示的实现方法
Sep 28 Python
python实现人脸识别代码
Nov 08 Python
Python 2.7中文显示与处理方法
Jul 16 Python
使用python opencv对目录下图片进行去重的方法
Jan 12 Python
redis数据库及与python交互用法简单示例
Nov 01 Python
Django 限制访问频率的思路详解
Dec 24 Python
在pycharm中为项目导入anacodna环境的操作方法
Feb 12 Python
Python OrderedDict字典排序方法详解
May 21 Python
浅谈tensorflow使用张量时的一些注意点tf.concat,tf.reshape,tf.stack
Jun 23 Python
Python return语句如何实现结果返回调用
Oct 15 Python
python如何查找列表中元素的位置
May 30 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数组函数序列 之shuffle()和array_rand() 随机函数使用介绍
2011/10/29 PHP
PHP基础教程(php入门基础教程)一些code代码
2013/01/06 PHP
深入phpMyAdmin的安装与配置的详细步骤
2013/05/07 PHP
深入PHP curl参数的详解
2013/06/17 PHP
php无限分类使用concat如何实现
2015/11/05 PHP
WordPress后台中实现图片上传功能的实例讲解
2016/01/11 PHP
简单实用的js调试logger组件实现代码
2010/11/20 Javascript
ejs v9 javascript模板系统
2012/03/21 Javascript
JS Date函数整理方便使用
2013/10/23 Javascript
jQuery实现简单隔行变色的方法
2016/02/20 Javascript
Bootstrap教程JS插件弹出框学习笔记分享
2016/05/17 Javascript
AngularJs自定义服务之实现签名和加密
2016/08/02 Javascript
easyui 中的datagrid跨页勾选问题的实现方法
2017/01/18 Javascript
webpack 打包压缩js和css的方法示例
2018/03/20 Javascript
微信小程序使用字体图标的方法
2019/05/23 Javascript
JavaScript TAB栏切换效果的示例
2020/11/05 Javascript
vue 在服务器端直接修改请求的接口地址
2020/12/19 Vue.js
[01:00] DOTA2英雄背景故事第五期之重力引力法则谜团
2020/07/16 DOTA
Python中的列表知识点汇总
2015/04/14 Python
Python实现PS滤镜的旋转模糊功能示例
2018/01/20 Python
Python实现字典排序、按照list中字典的某个key排序的方法示例
2018/12/18 Python
Django 日志配置按日期滚动的方法
2019/01/31 Python
Python小程序 控制鼠标循环点击代码实例
2019/10/08 Python
python实现QQ邮箱发送邮件
2020/03/06 Python
AVON雅芳官网:世界上最大的美容化妆品公司之一
2016/11/02 全球购物
印度第一网上礼品店:IGP.com
2020/02/06 全球购物
DELPHI中如何调用API,可举例说明
2014/01/16 面试题
电子商务专业实习生自我鉴定
2013/09/24 职场文书
十岁生日同学答谢词
2014/01/19 职场文书
质量提升方案
2014/06/16 职场文书
励志演讲稿600字
2014/08/21 职场文书
2015年加油站站长工作总结
2015/05/27 职场文书
python实现的web监控系统
2021/04/27 Python
磁贴还没死, 微软Win11可修改注册表找回Win10开始菜单
2021/11/21 数码科技
Docker部署Mysql8的实现步骤
2022/07/07 Servers
win10双系统怎么删除一个系统?win10电脑有两个系统删除一个的操作方法
2022/07/15 数码科技