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()
python 网络编程第三版的更多相关文章
- 【unix网络编程第三版】阅读笔记(五):I/O复用:select和poll函数
本博文主要针对UNP一书中的第六章内容来聊聊I/O复用技术以及其在网络编程中的实现 1. I/O复用技术 I/O多路复用是指内核一旦发现进程指定的一个或者多个I/O条件准备就绪,它就通知该进程.I/O ...
- 【unix网络编程第三版】阅读笔记(三):基本套接字编程
unp第三章主要介绍了基本套接字编程函数.主要有:socket(),bind(),connect(),accept(),listen()等. 本博文也直接进入正题,对这几个函数进行剖析和讲解. 1. ...
- 【unix网络编程第三版】阅读笔记(二):套接字编程简介
unp第二章主要将了TCP和UDP的简介,这些在<TCP/IP详解>和<计算机网络>等书中有很多细致的讲解,可以参考本人的这篇博客[计算机网络 第五版]阅读笔记之五:运输层,这 ...
- 【UNIX网络编程第三版】阅读笔记(一):代码环境搭建
粗略的阅读过<TCP/IP详解>和<计算机网络(第五版)>后,开始啃这本<UNIX网络编程卷一:套接字联网API>,目前linux下的编程不算太了解,在阅读的过程中 ...
- Python核心编程第三版第二章学习笔记
第二章 网络编程 1.学习笔记 2.课后习题 答案是按照自己理解和查阅资料来的,不保证正确性.如由错误欢迎指出,谢谢 1. 套接字:A network socket is an endpoint of ...
- 【unix网络编程第三版】ubuntu端口占用问题
<unix网络编程>一书中的代码并不是能直接运行,有时候需要结合各方面的知识来解决,大家在这本书的时候,一定要把代码都跑通,不难你会错过很多学习的机会! 1.问题描述 本人在阅读<U ...
- 【unix网络编程第三版】阅读笔记(四):TCP客户/服务器实例
本篇博客主要记录一个完整的TCP客户/服务器实例的编写,以及从这个实例中引发的对僵死进程的处理等问题. 1. TCP客户/服务器功能需求 本实例完成以下功能: (1) 客户从标准输入读入一行文本,并写 ...
- python 网络编程第四版
使用SocketServer 模块来完成服务端编程 1.服务端代码如下: #!/usr/bin/python #!coding:utf-8 import SocketServer as sockets ...
- 第十一章:Python の 网络编程基础(三)
本課主題 多线程的创建和使用 消息队列的介绍 Python 操作 memached 和 redis 实战 本周作业 消息队列的介绍 对列是在内存中创建的,如果整个进程里的程序运行完毕之后会被清空,消息 ...
随机推荐
- linux网络编程:使用单进程实现多客户端通信
服务端: //回射服务器 //避免僵尸进程 #include "unistd.h" #include "sys/types.h" #include " ...
- The port Command
The port Command help: port help selfupdate selfupdate: sudo port selfupdate search: port search tft ...
- jquery ajax 跨域提交(附IE浏览器解决方案)
后台输出内容之前需要指定header("Access-Control-Allow-Origin: *"); post 之前 jQuery.support.cors = true; ...
- MySQL与unix时间问题
1.select unix_timestamp() -->可以返回以秒记的unix时间. 2.select from_unixtime('1455804595','%Y年%m月%d号'); -- ...
- TCP协议: SYN ACK FIN RST PSH URG 详解
TCP的三次握手是怎么进行的了:发送端发送一个SYN=1,ACK=0标志的数据包给接收端,请求进行连接,这是第一次握手:接收端收到请求并且允许连接的话,就会发送一个SYN=1,ACK=1标志的数据包给 ...
- 【C/C++】Linux下system()函数引发的错误
http://my.oschina.net/renhc/blog/54582 [C/C++]Linux下system()函数引发的错误 恋恋美食 恋恋美食 发布时间: 2012/04/21 11:3 ...
- linux中的strings命令简介
摘自:http://blog.csdn.net/stpeace/article/details/46641069 linux中的strings命令简介 在linux下搞软件开发的朋友, 几乎没有不知道 ...
- #include <boost/regex.hpp>
boost C++的正则表达式库boost.regex可以应用正则表达式于C++.正则表达式大大减轻了搜索特定模式字符串的负担,在很多语言中都是强大的功能. boost.regex库中两个最重要的类是 ...
- Linux基本配置和管理 4 ---- Linux系统启动详解
1 系统启动的流程 BIOS -> MBR+boot code -> 执行引导程序: GRUB -> 加载内核 -> 执行init -> 运行runlevel 2 启动详 ...
- ORACLE AWR概述及生成AWR报告
1.Overview of the Automatic Workload Repository The Automatic Workload Repository (AWR) collects, pr ...