Python学习笔记(3):类、网路​​编程

Python系列的学习重点大概先写到这篇为止,其他基础的东西可以去翻翻官方文件,以后在实作上有遇到一些有趣的议题会再以文章来记录。


本篇主要介绍了类的定义与使用方法,只提及需要特别注意的重点,

枝微末节的就不提了,可以参考官方文件或以其他编程语言为参照,

基本上如果有其他编程语言的底子,Python的学习曲线还是相对较短的。


此次作为示例的代码是一个C/S架构的简易Echo Server,

服务端开启一个Accept线程监听外来连线,然后再针对已接受的外部连线开启一个子线程处理网路通信。

主线程根据键盘输入决定是否结束程序。


服务端: [ EchoServer.py ]

# -*- coding: UTF-8 -*-
# Description: It's a socket echo server.
#              Simply use working thread to process the communication from client.

import socket
import threading
import sys

HOST = ""
PORT = 5226
isRunning = 1
socketList = []


class ClientThread(threading.Thread):
    def __init__(self, ss, address):
        super().__init__()
        self.ss = ss
        self.addr = address

    def run(self):
        while isRunning:
            try:
                data = self.ss.recv(1024)
            except ConnectionResetError:
                print("Connection is closed by remote client...")
                break
            except ConnectionAbortedError:
                print("slave socket is closed.")
                break
            if (not data) or (data == "exit"):
                print("Client thread exit from", self.addr)
                break
            self.ss.send(data)
        self.ss.close()
        for s in socketList:  # remove client socket from socketList
            if s == self.ss:
                socketList.remove(s)
                break


class AcceptThread(threading.Thread):
    def __init__(self, s):
        threading.Thread.__init__(self)
        self.s = s

    def run(self):
        while isRunning:
            try:
                conn, addr = self.s.accept()
            except:
                print("Master socket is closed.")
                break
            print("Connected by", addr)
            socketList.append(conn)  # add client socket into socketList
            client_thread = ClientThread(conn, addr)
            client_thread.start()


def main():
    try:
        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0)
    except socket.error as msg:
        print(msg)
        print("Could not open socket!")
        sys.exit(1)
    try:
        s.bind((HOST, PORT))
        s.listen(5)
    except socket.error as msg:
        s.close()
        print(msg)
        print("Could not open socket!")
        sys.exit(1)
    if s is None:
        print("Could not open socket!")
        sys.exit(1)

    print("Server is listening on %d" % PORT)
    socketList.append(s)  # add master socket into socket list

    accept_thread = AcceptThread(s)
    accept_thread.start()
    while input("enter 'exit' to terminate program:") != "exit":  # wait for user input "exit"
        pass
    global isRunning
    isRunning = 0  # Set all thread being stop
    for sock in socketList:  # Close all socket
        sock.close()
    print("Exit server program.")

if __name__ == "__main__":
    try:
        main()
    except KeyboardInterrupt:
        sys.exit(2)
    except Exception as e:
        print(e)



重点1

上述代码先以threading这个模组实现了线程类,如第15行第42行的类定义:

ClientThread类和AcceptThread类继承了threading.Thread类,並且以__init__()函数定义了子类的constructor

这里需要注意的是第17行第44行,这两行代码都是在呼叫父类的constructor,只不过第44行是旧版Python的写法,第17行是Python 3.0之后的写法。


第17行使用super()呼叫避免直接使用父类名称,关于super()这个概念可以参考Java。

第17行的新版写法同时还可以改写成以下的旧版写法:

super(ClientThread, self).__init__()

相形之下就显得啰嗦。


------


重点2

线程类中必须实现一个run()函数,以供线程启动后运行,如第21行第47行


------


重点3

main()函数中绑定socket、监听socket我就不多解释了,这些用法可以参考C/C++的socket编程。

重点在第82、83行

第82行宣告了一个AcceptThread类的对象实例,并且将本地的master socket作为参数传入类的建构函数中,

再赋值给类的成员变量self.s,作为接受外部连线之用。

第83行用AcceptThread类的对象实例呼叫了start()函数启动了AcceptThread线程。


重点大概就是这些了,其它的枝微末节可以边看代码边理解,很简单的。

最后附上客户端代码。


------


客户端: [ EchoClient.py ]

# -*- coding: UTF-8 -*-
# Description: It's a socket echo client

import socket
import sys

HOST = "your.domain.net"
PORT = 5226
s = None

for res in socket.getaddrinfo(HOST, PORT, socket.AF_UNSPEC, socket.SOCK_STREAM, 0, socket.AI_PASSIVE):
    af, socktype, proto, canonname, sa = res
    try:
        s = socket.socket(af, socktype, proto)
    except socket.error as msg:
        print(msg)
        s = None
        continue
    try:
        s.connect(sa)
    except socket.error as msg:
        print(msg)
        s.close()
        s = None
        continue
    break
if s is None:
    print("Could not open socket!")
    sys.exit(1)

while 1:
    data = ""
    while data == "":
        data = input("Input math algorithm:")
    if data == "exit":
        break
    try:
        s.send(data.encode())
        data = s.recv(1024)
    except:
        print("Connection is closed by remote server...")
        break
    print("recv:%s" % data.decode())

s.close()
print("Exit client program.")


好了,今天就写到这边,我肚子疼,先屎遁去洗手间。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值