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程序设计有所帮助。
0
投稿
猜你喜欢
- 一、网络请求在uni中可以调用uni.request方法进行请求网络请求需要注意的是:在小程序中网络相关的 API 在使用前需要配置域名白名
- python中基本数据类型和其他的语言占用的内存空间大小有很大差别import sysa = 100b = Truec = 100Ld =
- 方案5 使用xml参数 对sql server xml类型参数不熟悉的童鞋需要先了解下XQuery概念,这里简单提下XQuery 是用来从
- 本文实例讲述了PHP函数shuffle()取数组若干个随机元素的方法。分享给大家供大家参考,具体如下:有时候我们需要取数组中若干个随机元素(
- pylint是一个不错的代码静态检查工具。将其配置在pycharm中,随时对代码进行分析,确保所有代码都符合pep8规范,以便于养成良好的习
- 酝酿了将近一个春夏秋冬的腾讯网首页终于亮剑!反响热烈!让我们来分享它成功背后的酸甜苦辣吧。腾讯网首页改版终于开花结果。于2008年3月25日
- 代码如下:USE [tempdb] GO /****** Object: UserDefinedFunction [dbo].[fun_ge
- 一、什么是jieba库jieba是优秀的中文分词第三方库,由于中文文本之间每个汉字都是连续书写的,我们需要通过特定的手段来获得其中的每个词组
- 用ASP实现搜索引擎的功能是一件很方便的事,可是,如何实现类似3721的智能搜索呢?比如,当在搜索条件框内输入“中国人民”时,自动从中提取“
- 前言在搜集了很多文本语料之后,会开始漫长的数据清洗过程,通常要不断迭代。1. 问题描述有些文本数据中,会包含一些特殊符号。猜想可能是从某些富
- python基础pandas的drop()用法做数据处理得时候用到了pandas,体验不错,记录如下:import pandas as pd
- 在MySQL数据库中导出整个数据库:1.导出整个数据库mysqldump -u 用户名 -p 数据库名 > 导出的文件名mysqldu
- 前言最近在研究怎么对图片资源进行无损压缩,网上也找了一些资料。总而言之,收获不少,所以想对最近的学习做个总结。无损压缩其实是相对而言的,目的
- 在项目开发中,经常出现这样的需求。在新增或修改一个主表数据时,对应的从表也要进行同步,此时我们是怎么操作的了?典型的方法就是对于主表的各数据
- 部署网站前查看一下系统是否已经安装CGI1、启动iis服务器,打开IIS服务器打开IIS服务器,点击网站,右击“添加网站”2、创建网站点击“
- 熬了半个通宵,写出了自己的Google SiteMap文件,在这里给出详细编写教程,愿对大家有所帮助。Google SiteMap的作用及协
- 本文实例讲述了Python计算两个日期相差天数的方法。分享给大家供大家参考,具体如下:#!/usr/bin/pythonimport tim
- Brendan Eich于1995年在Netscape发明了JavaScript语言,这个语言在过去的一些年里曾是一个最被误解的语言,随着A
- 引子之前在学习Django时,对状态保持这个概念很模糊,不知道怎么去保持,通过什么去实现保持都不太清楚;稍微花了一点时间,把状态保持的基本概
- 记得以前写过一篇文章 php有效的过滤html标签,js代码,css样式标签: <?php $str = preg_replace(