1. Dynamic Import Module
Lib = __import__ ("Lib.aa")
c = LIB.AA.C ()
Print (C.name)
Print (LIB.AA.C)
#下面为常用的方法
Import importlib
lib = importlib.import_module (' Lib.aa ')
Print (lib. C ())
2. Assertions
Example 1:
Assert 6 = = 6
Print (' 6 equals 6 ')
There's no problem with the appeal, so I'll print the following.
Example 2:
Assert type (2) is STR
Print (' G is str ')
The above example assertion is not valid, so it will be an error
Traceback (most recent):
File "/users/luobiao/pycharmprojects/s14/day7/lib/aa.py", line, in <module>
Assert type (2) is STR
Assertionerror
3. Write a simple SOCKETSSH client
1) server-side code
import socket,os,time
#申明socket
Server = Socket.socket ()
#进行ip绑定
server.bind (' localhost ', 6767)
#进行监听
Server.listen ()
#进行循环等待 and receive connection and connection addresses
while True:
Conn,addr = server.accept ()
pri NT (CONN,ADDR)
while True:
#进行数据接收
data = CONN.RECV (1024x768)
#进行数据打印 and Decode
Print (Data.decode ())
#调用os的popen模块 for command invocation
D = Os.popen (Data.decode ()). Read ()
length = str (Len (D.encode ()))
If Len (d) = = 0:
d = ' Command error, please re-enter. '
#回传消息
Conn.send (Length.encode (encoding= ' utf-8 '))
messages sent #socket粘包 two times are glued together, resulting in receive errors
#解决方案1: Use sleep for 0.5 seconds to get the buffer time-out so that two times the data is not in the same buffer
#time. Sleep (0.5)
#解决方案2:
Client_ack = conn.re CV (1024x768) #接收一个数据, let the client return information
Print (Client_ack.decode ())
Conn.send (D.encode (encoding= ' utf-8 '))
Ser Ver.close ()
2) Customer Terminal code
Import Socket
#申明socket
Client = Socket.socket ()
#指定连接地址和端口
Client.connect ((' localhost ', 6060))
#循环发送给信息
While True:
#让用户输入信息, imitating SSH input
message = input (' >>: ')
#发送消息, converts data of type str to bytes and converts it using encode
Client.send (Message.encode (encoding= ' utf-8 '))
#接收消息 and specifies the maximum amount of data to receive, in bytes
Length = Client.recv (1024x768). Decode ()
Client.send (' received size '. Encode (encoding= ' utf-8 '))
Print (length)
#打印消息并进行转码
#print (Length.decode ())
Recevied_size = 0
while int (length) > recevied_size:
data = CLIENT.RECV (1024)
Recevied_size +=len (Data.decode ())
Print (Data.decode ())
Print (recevied_size)
Else
Print (' Receive complete ')
#关闭链接
Client.close ()
The path of Python Day8