python 网络编程第三版

为服务端增加多线程解决方案

1、服务端代码如下:

***这个版本并没有真正的起到多线程的作用,主要原因在于t.join();以后的版本会改进这个问题***

#!/usr/bin/python
#!coding:utf-8

import os,sys,time
from socket import *
import threading

def handleClient(conn):
    print ‘[info]    handleClient is :{0}‘.format(os.getpid())
    while True:
        data = conn.recv(1024)
        if not data : print ‘[info]    handleClient client is stoped ..‘;break
        print ‘[info]    handleClient recive this --> {0}‘.format(data.encode())
        reply=‘[info]    handleClient this is the information from server --> {0}‘.format(data.decode())
        conn.send(reply.encode())
    conn.close()
    sys.exit(0)

if __name__ == "__main__":
    hostIp=‘127.0.0.1‘
    port=2048
    sock=socket(AF_INET,SOCK_STREAM)
    sock.bind((hostIp,port))
    sock.listen(5)
    print ‘[info]    parent pid is :{0} start listen {1}‘.format(os.getpid(),(hostIp,port))
    while True:
        conn,addr=sock.accept()
        print ‘[info]    parent get a client {0}‘.format(addr)
        t=threading.Thread(target=handleClient,args=(conn,))
        t.start()
        t.join()

2、客户端代码如下:

#!/usr/bin/python
#!coding:utf-8

from socket import *
import os,sys

if __name__ == "__main__":
    #定义套接字
    hostIp=‘127.0.0.1‘
    port=2048
    sock=socket(AF_INET,SOCK_STREAM)
    messages=[‘hello I am a client‘]
    messages=messages+sys.argv[1:]
    sock.connect((hostIp,port))
    print ‘[info]    已经连接到server ‘

    for message in messages:
        sock.send(message.encode())
        print sock.recv(1024).decode()
    sock.close()
    
时间: 2024-08-25 15:55:40

python 网络编程第三版的相关文章

【unix网络编程第三版】阅读笔记(五):I/O复用:select和poll函数

本博文主要针对UNP一书中的第六章内容来聊聊I/O复用技术以及其在网络编程中的实现 1. I/O复用技术 I/O多路复用是指内核一旦发现进程指定的一个或者多个I/O条件准备就绪,它就通知该进程.I/O复用适用于以下场合: (1) 当客户处理多个描述符(一般是交互式输入或网络套接字),必须适用I/O复用 (2) 当一个客户处理多个套接字时,这种情况很少见,但也可能出现 (3) 当一个TCP服务器既要处理监听套接字,又要处理已连接套接字,一般就要使用I/O复用 (4) 如果一个服务器既要适用TCP,

Unix网络编程第三版源码编译

配置: $ cd Unix-Network-Programming/ $ chmod 755 configure $ ./configure 主要的工作是检查系统是否有源码编译所依赖的各种资源(系统版本是否匹配.编译器.库文件.头文件以及结构体定义等等) checking build system type... x86_64-unknown-linux-gnu checking host system type... x86_64-unknown-linux-gnu checking for

分享《Python核心编程(第三版)》(高清中文版PDF+高清英文版PDF+源代码)

下载:https://pan.baidu.com/s/1-6muQ-DPjNu_1Rqg5kRHzQ <Python核心编程(第三版)>(高清中文版PDF+高清英文版PDF+源代码) 高清中文版667页,带目录和书签,文字能够复制粘贴. 高清英文版886页,带目录和书签,文字能够复制粘贴. 配套源代码. 其中高清中文版如图 原文地址:http://blog.51cto.com/14050756/2319076

分享《Python核心编程(第三版)》+PDF+源码+Wesley Chun+孙波翔 李斌 李晗

下载:https://pan.baidu.com/s/1ilaTxGsh4_Ko--5oU2zvKA 更多资料分享:http://blog.51cto.com/14087171 <Python核心编程(第三版)>(高清中文版PDF+高清英文版PDF+源代码) 高清中文版667页,带目录和书签,文字能够复制粘贴. 高清英文版886页,带目录和书签,文字能够复制粘贴. 配套源代码. 其中高清中文版如图 原文地址:http://blog.51cto.com/14087171/2321611

Python网络编程03/ low版解决粘包问题

目录 Python网络编程03/ low版解决粘包问题 1.操作系统的缓存区 2.基于TCP协议的socket循环通信 2.1 服务端(server) 2.2客户端(client) 3.基于TCP协议的socket链接+循环 通信 3.1服务端(server) 3.2 客户端(client) 4.基于TCP协议的socket应用实例:执行远程命令 4.1服务端(server) 4.2客户端(client) 5.粘包现象 5.1服务端(server) 5.2客户端(client) 5.3展示收发问

Python核心编程第三版第二章学习笔记

第二章 网络编程 1.学习笔记 2.课后习题 答案是按照自己理解和查阅资料来的,不保证正确性.如由错误欢迎指出,谢谢 1. 套接字:A network socket is an endpoint of a connection across a computer network,Sockets are often represented internally as simple integers, which identify which connection to use. 套接字是网络通信的

unix网络编程第三版源代码ubuntu下配置的问题解决

第一步:首先下载本书配套的源码unpv13e.tar.gz 第二步:解压后进入根文件夹有一个README 4 Execute the following from the src/ directory: 5 6 ./configure # try to figure out all implementation differences 7 8 cd lib # build the basic library that all programs need 9 make # use "gmake&q

第十一章:Python の 网络编程基础(三)

本課主題 多线程的创建和使用 消息队列的介绍 Python 操作 memached 和 redis 实战 本周作业 消息队列的介绍 对列是在内存中创建的,如果程序运行完毕之后被清空了,消息就清空了. 先进先出队列 class Queue: '''Create a queue object with a given maximum size. If maxsize is <= 0, the queue size is infinite. ''' def __init__(self, maxsize

(转)[Python 网络编程] makefile (三)

socket.makefile(mode ='r',buffering = None,*,encoding = None,errors = None,newline = None )返回一个与套接字相关联的文件对象.返回的确切类型取决于给makefile()提供的参数. 这些参数的解释方式与内置open()函数的解释方式相同,除了makefile方法唯一支持的mode值是'r'(默认)'w'和'b'. 套接字必须处于阻塞模式; 它可能有超时,但是如果超时发生,文件对象的内部缓冲区可能会以不一致的