UDP Chat (Using Python)
来源:互联网 发布:正规网络医生兼职平台 编辑:程序博客网 时间:2024/05/22 06:27
Foreword:
This is my note, and maybe many mistakes in it. So don’t take it as a “book” :)
sys.stdin
1. what?
I’m not familiar with “sys.stdin” so I google it, and this is an answer on stackoverflow:
Are you familiar with input()? Wherever input() would take input from, that’s sys.stdin. That may be a terminal, or it may be directed from a file, or it might be coming from another program’s output, or it might be something else, depending on how you invoked the program.
2. usage?
In:
lines = sys.stdin.readlines()
sys.stdin can be regarded as a file, and “.readlines()” reads the lines from this file into a list.
select.select( rlist, wlist, xlist, [timeout] )
Select is used to monitor sockets, files and pipes, waiting for I/O completion.
This method wait (block) until some objects of each parameter in is ready, and it returns a triple of lists of objects that are ready.
Look: i,o,e = select.select([sys.stdin],[],[],0.0001)
input
sys.stdin is the list that to be selected from, once some thing in it is ready, method returns.
output
i: objects that are ready for reading
o: objects that are ready for writing
e: objects that are ready for “exceptional condition”
Socket
1. socket类型
socket.AF_UNIX:用于同一台机器上的进程通信(既本机通信)
socket.AF_INET:用于服务器与客户机之间的网络通信
socket.AF_INET6:基于IPV6方式的服务器与服务器之间的网络通信
socket.SOCK_STREAM:基于TCP的流式socket通信
socket.SOCK_DGRAM:基于UDP的数据报式socket通信
socket.SOCK_RAW:原始套接字,普通的套接字无法处理ICMP、
IGMP等网络报文,而SOCK_RAW可以;其次SOCK_RAW也可以处理特殊的IPV4报文;此外,利用原始套接字,可以通过IP_HDRINCL套接字选项由用户构造IP头
socket.SOCK_SEQPACKET:可靠的连续数据包服务
创建UDP socket的方式: sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
2. socket函数
1. TCP发送数据时,已建立好TCP链接,所以不需要指定地址,而UDP是面向无连接的,每次发送都需要指定发送给谁。
2. 服务器与客户端不能直接发送列表,元素,字典等带有数据类型的格式,发送的内容必须是字符串数据。
s.bind(address)
将套接字绑定到地址。在AF_INET下,以tuple(host, port)的方式传入,如: s.bind((host, port))
s.recvfrom(bufsize[, flag])
接受UDP套接字的数据,返回值是tuple(data, address)。其中data是包含接受数据的字符串,address是发送数据的套接字地址
print error code
try: s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)except socket.error as msg: print("Failed to creat socket. Error code: " + str(msg[0]) + ", Error message: " + msg[1]) sys.exit()
Tkinter
1. 如何规定窗口大小及位置
width = root.winfo_screenwidth()height = root.winfo_screenheight()root.geometry('%sx%s+%s+%s' % (width, height, x_offset, y_offset))
最后一句确定了窗口的具体大小,其中offset目测是规定界面显示在屏幕中的位置(与屏幕左右、上下的距离)。
注意:place,grid和pack这三种方法在同一个master window中只能使用一种,混着用会导致程序无法执行!(什么是master window我还不是很懂……)
2. StringVar()
使用场景:需要经常手动修改文字显示的Entry或label
使用方法:
self.nameVar = StringVar()self.nameVar.set("SDH")nameField = Entry(ipGroup, width=10, textvariable=self.nameVar)
其中nameVar是一个字符串类型的变量,我们给它赋了初值“SDH”,并将其与名为“nameField”的Entry进行绑定,那么nameField这个Entry就变成了一个可以随意改值的Entry。
本质上,我们还是画了一个Entry出来,只不过我们使这个Entry的内容成了可以改变的量,为此我们需要使用textvariable这个属性,并定义一个字符串变量来赋给textvariable。
运行结果如下:
3. Text self.receivedChats = Text(readChatGroup, bg="white", width=60, height=30, state=DISABLED)
state=DISABLED控制使得text框内的内容不可编辑。
Python
1. replace()方法
str.replace(old, new[, max])
作用:将原字符串中所有old替换成new,第三个字节规定最多替换max次
2. dictionary类型
dict = {}dict['one'] = "This is one"dict[2] = "This is two"
dictionary类型定义的列表的每一个元素都是一个键值对,如上面键one对应值”This is one”。
- UDP Chat (Using Python)
- UDP之(chat)
- python -- chat (sample)
- A Chat Application Using Asynchronous TCP Sockets
- A Chat Application Using Asynchronous TCP Sockets
- CHAT
- CHAT
- chat
- chat
- chat
- chat
- chat
- python 网络编程(udp)
- 面试总结---chat room的UDP聊天室软件
- Using Python
- hdu 5071 Chat(模拟)
- HDU 5071 Chat (模拟)
- HDU 5071 Chat(模拟)
- database:alter mysql
- 简单的有限状态机Unity独家写法(呸~厚颜无耻之人)
- JavaWeb自主学习--泛型,day5
- TensorFlow安装使用入门
- ganglia简介以及工作原理和配置
- UDP Chat (Using Python)
- mysql语句优化总结
- 冬至为什么要吃饺子
- npm更新 、nodejs更新
- [GPU] Ubuntu安装 NVIDIA GPU 驱动
- git笔记
- JDBC知识点
- android studio中productFlavors配置
- 30分钟掌握ES6/ES2015核心内容