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 8021191
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T21:54:38+00:00 2026-06-04T21:54:38+00:00

I have a Twisted socket that I am attempting to run on multiple ports.

  • 0

I have a Twisted socket that I am attempting to run on multiple ports. The following code has worked for me before, but that was around 1 month ago as I have not touched the code since, if I remember right. Now, after re-entering the code into my Twisted program, it doesn’t work anymore.

class Socket(Protocol):
    table = Table()

    def connectionMade(self):
        #self.transport.write("""connected""")
        self.factory.clients.append(self)
        print "Clients are ", self.factory.clients

    def connectionLost(self, reason):
        self.factory.clients.remove(self)

    def dataReceived(self, data):
        #print "data is ", data
        a = data.split(':')
        if len(a) > 1:
            command = a[0]
            content = a[1]

            if command == "Number_of_Players":
                msg = table.numberOfPlayers


        print msg

        for c in self.factory.clients:
                c.message(msg)

    def message(self, message):
        self.transport.write(message)

NUM_TABLES = 10

factories = [ ]
for i in range(0, NUM_TABLES):
    print i
    factory = Factory()
    factory.protocol = Socket
    factory.clients = []
    factories.append(factory)
    reactor.listenTCP(1025+i, factory)
    #print "Blackjack server started"
    reactor.run()

It would usually print Blackjack server started however many times in the range I set, but now it doesn’t. To test if it was looping over or not, I set out to print i, but it only printed 0. For some reason, the for loop only looped 1 time.

Any suggestions? Thanks!

  • 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-04T21:54:40+00:00Added an answer on June 4, 2026 at 9:54 pm

    Twisted programs usually have only one reactor running. Keep in mind that when you start (.run()) the reactor, execution passes inside the reactor loop (and the various events that you have defined in your code, like connectionMade(), connectionLost(), dataReceived(), etc are triggered when the respective actions take place). Whatever code is after the reactor.run() is executed only after reactor is stopped.

    So, your code never passes the first iteration of the for loop.

    Try moving the reactor.run() out of the loop:

    NUM_TABLES = 10
    
    factories = [ ]
    for i in range(0, NUM_TABLES):
        print i
        factory = Factory()
        factory.protocol = Socket
        factory.clients = []
        factories.append(factory)
        reactor.listenTCP(1025+i, factory)
    
    # print "Blackjack server started"
    
    reactor.run()
    
    # whatever code you put here, is executed only **after reactor has stopped**
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some Twisted code which creates multiple chains of Deferreds. Some of these
I have the following python code using the twisted API. def function(self,filename): def results(result):
I have a small GTK python application that imports a package (Twisted) that may
I'm using twisted. I have my protocols set up so that, to send an
I have an existing program that has its own main loop, and does computations
I have a class that inherits from twisted.internet.protocol.DatagramProtocol class. In my startProtocol() implementation I
I know that my scenario is a little bit twisted. I have come across
I have the following code (almost an exact copy of the Chat server example
I have the following html: <div class=canvas red> <div> <div class=canvas blue> <div> has
I have an xmlrpc server using Twisted. The server has a huge amount of

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.