Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8453315
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T11:44:21+00:00 2026-06-10T11:44:21+00:00

Python newbie here. I’m writing an SMTP server using Twisted and twisted.mail.smtp. I’d like

  • 0

Python newbie here. I’m writing an SMTP server using Twisted and twisted.mail.smtp. I’d like to log incoming connections and possibly dump them when there are too many concurrent connections. Basically, I want ConsoleMessageDelivery.connectionMade() method to be called in the following, when a new connection is made:

class ConsoleMessageDelivery:
    implements(smtp.IMessageDelivery)

    def connectionMade(self):
        # This never gets called

    def receivedHeader(self, helo, origin, recipients):
        myHostname, clientIP = helo
        headerValue = "by %s from %s with ESMTP ; %s" % (myHostname, clientIP, smtp.rfc822date())
        # email.Header.Header used for automatic wrapping of long lines
        return "Received: %s" % Header(headerValue)

    def validateFrom(self, helo, origin):
        # All addresses are accepted
        return origin

    def validateTo(self, user):
        if user.dest.local == "console":
            return lambda: ConsoleMessage()
        raise smtp.SMTPBadRcpt(user)

class ConsoleMessage:
    implements(smtp.IMessage)

    def __init__(self):
        self.lines = []

    def lineReceived(self, line):
        self.lines.append(line)

    def eomReceived(self):
        return defer.succeed(None)

    def connectionLost(self):
        # There was an error, throw away the stored lines
        self.lines = None

class ConsoleSMTPFactory(smtp.SMTPFactory):
    protocol = smtp.ESMTP

    def __init__(self, *a, **kw):
        smtp.SMTPFactory.__init__(self, *a, **kw)
        self.delivery = ConsoleMessageDelivery()

    def buildProtocol(self, addr):
        p = smtp.SMTPFactory.buildProtocol(self, addr)
        p.delivery = self.delivery
        return p
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T11:44:23+00:00Added an answer on June 10, 2026 at 11:44 am

    connectionMade is part of twisted.internet.interfaces.IProtocol, not part of twisted.mail.smtp.IMessageDelivery. There’s no code anywhere in the mail server implementation that cares about a connectionMade method on a message delivery implementation.

    A better place to put per connection logic is in the factory. And specifically, a good way to approach this is with a factory wrapper, to isolate the logic about connection limits and logging from the logic about servicing SMTP connections.

    Twisted comes with a few factory wrappers. A couple in particular that might be interesting to you are twisted.protocols.policies.LimitConnectionsByPeer and twisted.protocols.policies.LimitTotalConnectionsFactory.

    Unfortunately, I don’t know of any documentation explaining twisted.protocols.policies. Fortunately, it’s not too complicated. Most of the factories in the module wrap another arbitrary factory to add some piece of behavior. So, for example, to use LimitConnectionsByPeer, you do something like this:

    from twisted.protocols.policies import LimitConnectionsByPeer
    ...
    factory = ConsoleSMTPFactory()
    wrapper = LimitConnectionsByPeer(ConsoleSMTPFactory(...))
    reactor.listenTCP(465, wrapper)
    

    This is all that’s needed to get LimitConnectionsByPeer to do its job.

    There’s only a little bit more complexity involved in writing your own wrapper. First, subclass WrappingFactory. Then implement whichever methods you’re interested in customizing. In your case, if you want to reject connections from a certain IP, that would mean overriding buildProtocol. Then, unless you also want to customize the protocol that is constructed (which you don’t in this case), call the base implementation and return its result. For example:

    from twisted.protocols.policies import WrappingFactory
    
    class DenyFactory(WrappingFactory):
        def buildProtocol(self, clientAddress):
            if clientAddress.host == '1.3.3.7':
                # Reject it
                return None
             # Accept everything else
             return WrappingFactory.buildProtocol(self, clientAddress)
    

    These wrappers stack, so you can combine them as well:

    from twisted.protocols.policies import LimitConnectionsByPeer
    ...
    factory = ConsoleSMTPFactory()
    wrapper = LimitConnectionsByPeer(DenyFactory(ConsoleSMTPFactory(...)))
    reactor.listenTCP(465, wrapper)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Python newbie here: I'm writing a market simulation in Python using Pysage, and want
I am a newbie in Python. I try to work with server using Thrift
python newbie here. I'm writing the code to control an experiment that has multiple
I'm a Python(3.1.2)/emacs(23.2) newbie teaching myself tkinter using the pythonware tutorial found here .
Python newbie here. I'm writing a script that can dump some output to either
Python newbie here. I was trying to troubleshoot an issue with writing a csv
I'm a python newbie and starting out with using the Bottle web framework on
As a newbie to Python, and mainly having a background of writing scripts for
Python newbie here and wanted to know if there was a way to combine
I'd like to search for tracks on iTunes using a Python script on Mac

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.