partition() 方法用来根据指定的分隔符将字符串进行分割。
如果字符串包含指定的分隔符,则返回一个3元的元组,第一个为分隔符左边的子串,第二个为分隔符本身,第三个为分隔符右边的子串。
flask源代码的run模块中,出现的代码当做示例
def run(): ...... _host = '127.0.0.1' _port = 5000 server_name = self.config.get('SERVER_NAME') sn_host, sn_port = None, None if server_name: sn_host, _, sn_port = server_name.partition(':')
SERVER_NAME 是在setting文件设置的
SERVER_NAME = '127.0.0.1:6666'
将SERVER_NAME拆分开,我们简化Flask的源代码
server_name = "127.0.0.1:6666"s_host, _, s_port = server_name.partition(":")print(s_host, s_port)print(s_host, _, s_port)结果 >>>>127.0.0.1 6666127.0.0.1 : 6666
Tags: None
Archives QR Code
标签:技巧,Python,partition,server,0.1,host,port,name From: https://www.cnblogs.com/q-q56731526/p/17346503.html