add new function

This commit is contained in:
yumoqing 2020-07-12 09:57:59 +08:00
parent e71a88a158
commit 531851ac34

View File

@ -2,12 +2,18 @@ import os
import time import time
import threading import threading
import sys import sys
from socket import AF_INET,SOCK_STREAM,socket import socket
from mylog import mylog from mylog import mylog
def logit(s) : def logit(s) :
mylog(__file__ + ':' + s) mylog(__file__ + ':' + s)
def get_free_local_addr():
with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as s:
s.connect(("8.8.8.8", 80))
return s.getsockname()
# return (ip,port)
class background(threading.Thread) : class background(threading.Thread) :
def __init__(self,func,kw) : def __init__(self,func,kw) :
threading.Thread.__init__(self) threading.Thread.__init__(self)
@ -45,7 +51,7 @@ class SocketServer(threading.Thread) :
def setSocketServer(self) : def setSocketServer(self) :
try : try :
self.sock = socket(AF_INET,SOCK_STREAM) self.sock = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
self.sock.bind((self.host,self.port)) self.sock.bind((self.host,self.port))
self.sock.listen(self.max_c) self.sock.listen(self.max_c)
self.ready = True self.ready = True
@ -93,7 +99,7 @@ class SocketClient :
def connect(self) : def connect(self) :
try : try :
self.sock = socket(AF_INET,SOCK_STREAM) self.sock = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
self.sock.connect((self.host,self.port)) self.sock.connect((self.host,self.port))
self.ready = True self.ready = True
except Exception as e: except Exception as e: