Tips: 本文创建于2016年7月13日,已超过 2 年,内容或图片可能已经失效!

python io阻塞

环境:只有代理能上网,局域网内的smtplib通过代理发邮件,下面是代码的大概内容

server

import socket  
import sys  
HOST = '0.0.0.0'  
PORT = 8888  
BUFSIZE = 1024  
MAXL = 10  
  
servsock = socket.socket(socket.AF_INET,socket.SOCK_STREAM)  
servsock.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1)    #端口重用  
servsock.bind((HOST,PORT))  
servsock.listen(MAXL)  
  
def server_io():  
    try:  
        while(1):  
            clisock,addre = servsock.accept()  
            while(1):  
                message = clisock.recv(BUFSIZE)  
                if not message:  
                    break  
                deal_mess(addre[0],message)  
        clisock.close()  
        servsock.close()  
    except Exception, e:   
        sys.exit(1)  
  
def deal_mess(addre,message):  
    try:  
        # 这里是想要做的事,addre是IP,message是内容,最终也要return  
    except Exception, e:  
        return  #返回隧道继续监听 

client

import socket  
import os   
import sys  
  
def client_io(message): #message为接受的参数  
    HOST = '127.0.0.1'  #server的ip  
    PORT = 8888  
    BUFSIZE = 1024  
    ADDR = (HOST,PORT)  
    try:  
        clisock = socket.socket(socket.AF_INET,socket.SOCK_STREAM)  
        clisock.connect(ADDR)  
        clisock.sendall(message)    
        clisock.close()  
    except Exception, e:  
        sys.exit()