from graphviz import Digraph
# 创建一个有向图
dot = Digraph(comment='FTP Protocol State Machine')
# 定义状态
states = {
'220': 'LightFTP server ready',
'200': 'Command okay / Always in UTF8 mode',
'331': 'User webadmin OK. Password required',
'230': 'User logged in, proceed.',
'150': 'File status okay; about to open data connection.',
'226': 'Transfer complete. Closing data connection.',
'221': 'Goodbye!'
}
# 添加节点
for state, desc in states.items():
dot.node(state, f'{state}\n{desc}')
# 添加边 (状态转移)
transitions = [
('220', '200', 'OPTS UTF8 ON'),
('200', '331', 'USER webadmin'),
('331', '230', 'PASS 222'),
('230', '200', 'PORT 192,168,182,1,195,206'),
('200', '150', 'LIST'),
('150', '226', ''),
('200', '221', 'QUIT'),
('226', '221', 'QUIT')
]
for from_state, to_state, label in transitions:
dot.edge(from_state, to_state, label)
# 保存并渲染图
dot.render('ftp_protocol_state_machine', format='png', cleanup=True)