Python简单网络编程示例【客户端与服务端】
作者:jihite 发布时间:2023-12-07 10:26:55
标签:Python,网络编程
本文实例讲述了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程序设计有所帮助。


猜你喜欢
- 1.什么是变量所谓变量,是指程序运行过程中其值可以改变的量。举例:在数学中x和y就是变量,Python中不同的是变量不只是存储数字,它可以存
- 本文实例讲述了python操作mongodb根据_id查询数据的实现方法。分享给大家供大家参考。具体分析如下:_id是mongodb自动生成
- Django根据已有数据库表反向生成models类一. 创建一个Django项目django-admin startproject ‘xxx
- python字符串连接的方法,一般有以下三种:方法1:直接通过加号(+)操作符连接website = 'python' +
- 1.VUE验证邮箱export const isEmail = (s) => { return /^([a-
- MySQL根据配置文件会限制Server接受的数据包大小。有时候大的插入和更新会受 max_allowed_packet 参数限制,导致写入
- 在网页制作中,表单中的对象总是给人一种单调与沉闷的感觉,比如说按钮、文本框等,它们一成不变的模样与颜色
- 设计方法曾经是个很尴尬的话题,因为经常看上去很美。专业人士们动手动脚折腾一大圈,出来的结果令人大跌眼镜。也有些设计师总喜欢把方法、概念吹的特
- Flask框架是Python开发的一个基于Werkzeug和Jinja 2的web开发微框架,它的优势就是极其简洁, 但又非常灵活,而且容易
- output输出打包后的代码,配置如何输出和输出位置在webpack.config中output包含以下属性:path:代码打包后要输出的位
- 记录:256写SQL最高境界:SELECT * FROM 表名。当然这是一句自嘲。探究一下SQL语句中JOIN的用法,直到经历这个场景,变得
- 程序还不是很精简,以后再修改,程序所用的数据库为-- “冯志宏”-- 所写的--“追捕”--软件中所带IP数据库和“国华软件 Guohua
- 今天看到everything搜索速度秒杀windows自带的文件管理器,所以特地模仿everything实现了文件搜索以及打开对应文件的功能
- 对单词最后一个字母的预测LSTM 的原理自己找,这里只给出简单的示例代码,就是对单词最后一个字母的预测。# LSTM 的原理自己找,这里只给
- 参考Stack Overflow Python: How to query multiple selected items in
- 一、 Scott用户下的表结构SCOTT。是在Oracle数据库中,一个示例用户的名称。其作用是为初学者提供一些简单的应用示例,不过其默认是
- 卷积和膨胀卷积在深度学习中,我们会碰到卷积的概念,我们知道卷积简单来理解就是累乘和累加,普通的卷积我们在此不做赘述,大家可以翻看相关书籍很好
- 目的是想通过给定一个ID,取出所有的子ID,包括子ID的子ID。一开始写成FUNCTION,因为FUNCTION调用方便,但是报错:ERRO
- 本文实例讲述了Python实现查找两个字典相同点的方法。分享给大家供大家参考,具体如下:问题:寻找两个字典中间相同的地方(相同的键、相同的值
- 归并排序思路:将数组不断二分,然后合并为有序数组C++实现:void mergeSort(T arr[], int left,int rig