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实现自动登录人人网并采集信息的方法
Jun 28 Python
Python制作爬虫采集小说
Oct 25 Python
Django URL传递参数的方法总结
Aug 28 Python
一些常用的Python爬虫技巧汇总
Sep 28 Python
用virtualenv建立多个Python独立虚拟开发环境
Jul 06 Python
详解python基础之while循环及if判断
Aug 24 Python
TensorFLow用Saver保存和恢复变量
Mar 10 Python
django ajax发送post请求的两种方法
Jan 05 Python
Python面向对象程序设计之类和对象、实例变量、类变量用法分析
Mar 23 Python
python调用摄像头的示例代码
Sep 28 Python
Python 求向量的余弦值操作
Mar 04 Python
python如何利用cv2模块读取显示保存图片
Jun 04 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数字和字符串ID互转函数(类似优酷ID)
2014/06/30 PHP
PHP改进计算字符串相似度的函数similar_text()、levenshtein()
2014/10/27 PHP
全面了解PHP中的全局变量
2016/06/17 PHP
微信 开发生成带参数的二维码的实例
2016/11/23 PHP
php use和include区别总结
2019/10/13 PHP
七种PHP开发环境搭建工具
2020/06/28 PHP
Lazy Load 延迟加载图片的 jQuery 插件
2010/02/06 Javascript
Javascript 自定义类型方法小结
2010/03/02 Javascript
jQuery 工具函数学习资料
2010/04/29 Javascript
jQuery Autocomplete自动完成插件
2010/07/17 Javascript
javascript中的作用域和上下文使用简要概述
2013/12/05 Javascript
jQuery实现瀑布流的取巧做法分享
2015/01/12 Javascript
jQuery源码分析之jQuery.fn.each与jQuery.each用法
2015/01/23 Javascript
js实现防止被iframe的方法
2015/07/03 Javascript
vue.js入门教程之绑定class和style样式
2016/09/02 Javascript
javascript动画之模拟拖拽效果篇
2016/09/26 Javascript
JavaScript实现鼠标滚轮控制页面图片切换功能示例
2017/10/14 Javascript
AngularJs用户输入动态模板XSS攻击示例详解
2018/04/21 Javascript
微信小程序带动画弹窗组件使用方法详解
2018/11/27 Javascript
vue-router 起步步骤详解
2019/03/26 Javascript
通过vue手动封装on、emit、off的代码详解
2019/05/29 Javascript
Vue中消息横向滚动时setInterval清不掉的问题及解决方法
2019/08/23 Javascript
python操作ie登陆土豆网的方法
2015/05/09 Python
在pandas中一次性删除dataframe的多个列方法
2018/04/10 Python
python3实现从kafka获取数据,并解析为json格式,写入到mysql中
2019/12/23 Python
Python实现动态给类和对象添加属性和方法操作示例
2020/02/29 Python
Python3实现打印任意宽度的菱形代码
2020/04/12 Python
keras 如何保存最佳的训练模型
2020/05/25 Python
localStorage、sessionStorage使用总结
2017/11/17 HTML / CSS
canvas绘制太极图的实现示例
2020/04/29 HTML / CSS
意大利灯具购物网站:Lampade.it
2018/10/18 全球购物
初一生物教学反思
2014/01/18 职场文书
信息学院毕业生自荐信范文
2014/03/04 职场文书
党的群众路线教育实践活动个人剖析材料
2014/10/07 职场文书
教师党员整改措施
2014/10/24 职场文书
2014年物业管理工作总结
2014/11/21 职场文书