玩蛇网提供最新Python编程技术信息以及Python资源下载!

python核心编程第二版第十六章的习题解答

python 培训

学习python时《python核心编程》是我的参考书,其中第二版第十六章的习题感觉比较有意思,我把我的解题方法拿出来晒晒,有不足之处请玩蛇网的大牛们帮我改改。

#!/usr/bin/env python

from socket import *
from time import ctime
import threading

HOST = ''
PORT = 21567
BUFSIZE = 1024
ADDR = (HOST, PORT)

def Deal(sck, username):
	while True:
		data = sck.recv(BUFSIZE)
		if data == "quit":
			del clients[username]	
			sck.send(data)
			sck.close()
			break
		for i in  clients.iterkeys():
			if i <> username:
				clients[i].send("[%s] %s: %s" %(ctime(), username, data))
			

chatSerSock = socket(AF_INET, SOCK_STREAM)
chatSerSock.bind(ADDR)
chatSerSock.listen(5)
#www.iplaypython.com

clients = {}

while True:
	print 'waiting for connection...'
	chatCliSock, addr = chatSerSock.accept()
	print "...connected romt: ", addr
	username = chatCliSock.recv(BUFSIZE)
	print username
	if clients.has_key(username):
		chatCliSock.send("reuse")
		chatCliSock.close()
	else:
		chatCliSock.send("success")
		clients[username] = chatCliSock
		t = threading.Thread(target=Deal, args=(chatCliSock, username))
		t.start()

chatSerSock.close()

第二部分代码

#!/usr/bin/env python
# _*_ coding: utf8 _*_

from socket import *
from time import ctime
import threading
import random
from sys import argv, exit, stdout
from getopt import gnu_getopt, GetoptError

help_info = ["cs.py [ -h | --help | -u | --username] username",
	"\t-h or --help\t显示帮助信息",
	"\t-u or --username\指定用户名"]
def help():
	for i in help_info:
		print i

def Send(sck, test):
	while True:
		data = raw_input('>')
		sck.send(data)
		if  data == "quit":
			break
def Recieve(sck, test):
	while True:
		data = sck.recv(BUFSIZ)
		if data == "quit":
			sck.close()
			break
		str = "\n" + data + "\n>" 
		stdout.write(str)

HOST = 'localhost'
PORT= 21567
BUFSIZ = 1024
ADDR = (HOST, PORT)
threads = []

if __name__ == "__main__":
	# 解析命令行参数
	try:
		opts, args = gnu_getopt(argv[1:], "hu:", ["help", "username="])		
	except GetoptError, err:
		print str(err)
		help()
		exit(2)
	username = ""
	for o, a in opts:
		if o in ("-h", "--help"):
			help()
			exit(0)
		elif o in ("-u", "--username"):
			username = a
		else:
			print "未知选项"
			help()
			exit(2)
	if username == "":
		help()
		exit(2)
	chatCliSock = socket(AF_INET, SOCK_STREAM)
	chatCliSock.connect(ADDR)
	chatCliSock.send(username)
	data = chatCliSock.recv(BUFSIZ)
	if data == "reuse":
		print "用户%s已登录" %(username)
		raw_input()
		exit(1)
	elif data == "success":
		print "用户%s成功登录" %(username)
		t = threading.Thread(target=Send, args = (chatCliSock, None))
		threads.append(t)
		t = threading.Thread(target=Recieve, args = (chatCliSock, None))
		threads.append(t)
		for i in range(len(threads)):
			threads[i].start()
		threads[0].join()

玩蛇网原创,转载请注明文章出处和来源网址:http://www.iplaypython.com/code/other/o2442.html



微信公众号搜索"玩蛇网Python之家"加关注,每日最新的Python资讯、图文视频教程可以让你一手全掌握。强烈推荐关注!

微信扫描下图可直接关注

玩蛇网Python新手QQ群,欢迎加入: ① 240764603 玩蛇网Python新手群
文章发布日期:2016-04-08 15:48 玩蛇网 www.iplaypython.com

评论列表(网友评论仅供网友表达个人看法,并不表明本站同意其观点或证实其描述)
相关文章推荐
别人正在看
特别推荐
去顶部去底部