Home | 简体中文 | 繁体中文 | 杂文 | Github | 知乎专栏 | 51CTO学院 | CSDN程序员研修院 | OSChina 博客 | 腾讯云社区 | 阿里云栖社区 | Facebook | Linkedin | Youtube | 打赏(Donations) | About
知乎专栏多维度架构

176.4. POP Sniffer

		
#!/usr/bin/python3
# Author: neo chan
# Homepage: http://netkiller.8800.org

import socketserver,sys
import threading

class ThreadedTCPRequestHandler(socketserver.BaseRequestHandler):

	def setup(self):
		print(self.client_address[0], 'connected!')
		self.request.send(b'+OK Welcome to coremail Mail Pop3 Server \r\n')

	def handle(self):
    	# self.request is the TCP socket connected to the client
		while True:
			self.data = self.request.recv(1024).strip()
			if self.data == b'QUIT':
				return
			if self.data == b'AUTH':
				self.request.send(b'-ERR Not support ntlm auth method\r\n')
			print("%s wrote: " % self.client_address[0])
			print (self.data)
			# just send back the same data, but upper-cased
			# self.request.send(self.data.upper())
			self.request.send(b'+OK 0 message(s) [0 byte(s)]\r\n')

	def finish(self):
		print( self.client_address[0], 'disconnected!')
		self.request.send(b'Goodbye! \r\n')

class ThreadedTCPServer(socketserver.ThreadingMixIn, socketserver.TCPServer):
	pass

if __name__ == "__main__":
	HOST, PORT = "172.16.0.1", 110

	# Create the server, binding to localhost on port 110
	# server = socketserver.TCPServer((HOST, PORT), MyTCPHandler)
	# server.serve_forever()

	# Activate the server; this will keep running until you
	# interrupt the program with Ctrl-C
	try:
		server = ThreadedTCPServer((HOST, PORT), ThreadedTCPRequestHandler)
		# Start a thread with the server -- that thread will then start one
		# more thread for each request
		server_thread = threading.Thread(target=server.serve_forever)
		# Exit the server thread when the main thread terminates
		# server_thread.setDaemon(True)
		server_thread.start()
	except KeyboardInterrupt:
		sys.exit(0)