Home | 簡體中文 | 繁體中文 | 雜文 | 打賞(Donations) | Github | OSChina 博客 | 雲社區 | 雲棲社區 | Facebook | Linkedin | 知乎專欄 | 視頻教程 | About

5.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)