#!/usr/bin/python3
# -*- coding:utf-8 -*-
import socket
import configparser
import _thread
import sys
def get_server_inf():
#创建cnf_prs对象
cnf_prs = configparser.ConfigParser()
#读取配置文件
cnf_prs.read("server.cnf")
#获取IP
server_ip = cnf_prs.get("server", "server_ip")
#获取端口
server_port = cnf_prs.get("server", "server_port")
server_inf=(server_ip, int(server_port))
return server_inf
#线程处理函数
def handle_msg(*uple):
while True:
client_msg = uple[0].recv(1024)
print(uple[1][0] + '-' +str(uple[1][1]) + ':send msg:'+ str(client_msg))
#创建服务端套接字
server_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
#设置套接字复用
server_sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
#获取ip、端口
server_inf=get_server_inf()
#绑定ip、port
server_sock.bind(server_inf)
server_sock.listen(5)
print("listen success...")
while True:
#返回的是列表
client_sock, client_addr = server_sock.accept()
print('PORT:'+str(client_addr[1])+':connect success')
client_inf=(client_sock, client_addr)
#print(client_inf)
#线程函数参数传递的是列表
_thread.start_new_thread(handle_msg, client_inf)