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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T23:11:16+00:00 2026-06-04T23:11:16+00:00

I slightly modified a server-client Twisted program on this site, which provided a program

  • 0

I slightly modified a server-client Twisted program on this site, which provided a program that could act as a server and a client (How to write a twisted server that is also a client?). I am able to connect the server-client to an external client on one side and an external server on the other. I want to transfer data from the external client to the external server via the server-client program. The problem I am having is getting the line received in the ServerProtocol class (in the server-client program) into the ClientProtocol class (in the server-client program). I have tried a number of ways of doing this, including trying to use the factory reference, as you can see from the def init but I cannot get it to work. (at the moment I am just sending literals back and forth to the external server and client) Here is the server-client code:

    from twisted.internet import protocol, reactor
    from twisted.protocols import basic


    class ServerProtocol(basic.LineReceiver):        
        def lineReceived(self, line):
            print "line recveived on server-client",line
            self.sendLine("Back at you from server-client")
            factory = protocol.ClientFactory()
            factory.protocol = ClientProtocol
            reactor.connectTCP('localhost', 1234, factory)

    class ClientProtocol(basic.LineReceiver):
        def __init__(self, factory):
            self.factory = factory

        def connectionMade(self):
            self.sendLine("Hello from server-client!")
            #self.transport.loseConnection()
        
        def lineReceived(self, line):
            print "line recveived on server-client1.py",line
            #self.transport.loseConnection()
          
    def main():
        import sys
        from twisted.python import log

        log.startLogging(sys.stdout)
        factory = protocol.ServerFactory()
        factory.protocol = ServerProtocol
        reactor.listenTCP(4321, factory)
        reactor.run()

    if __name__ == '__main__':
        main()

I should mention that I am able to connect to the server-client program with the external server and external client on ports 4321 and 1234 respectively and they just echo back. Also, I have not shown my many attempts to use the self.factory reference. Any advice or suggestions will be much appreciated.

  • 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-04T23:11:17+00:00Added an answer on June 4, 2026 at 11:11 pm

    This question is very similar to a popular one from the Twisted FAQ:

    How do I make input on one connection result in output on another?

    It doesn’t make any significant difference that the FAQ item is talking about many client connections to one server, as opposed to your question about one incoming client connection and one outgoing client connection. The way you share data between different connections is the same.

    The essential take-away from that FAQ item is that basically anything you want to do involves a method call of some sort, and method calls in Twisted are the same as method calls in any other Python program. All you need is to have a reference to the right object to call the method on. So, for example, adapting your code:

    from twisted.internet import protocol, reactor
    from twisted.protocols import basic
    
    class ServerProtocol(basic.LineReceiver):        
        def lineReceived(self, line):
            self._received = line
            factory = protocol.ClientFactory()
            factory.protocol = ClientProtocol
            factory.originator = self
            reactor.connectTCP('localhost', 1234, factory)
    
        def forwardLine(self, recipient):
            recipient.sendLine(self._received)
    
    class ClientProtocol(basic.LineReceiver):
        def connectionMade(self):
            self.factory.originator.forwardLine(self)
            self.transport.loseConnection()
    
    def main():
        import sys
        from twisted.python import log
    
        log.startLogging(sys.stdout)
        factory = protocol.ServerFactory()
        factory.protocol = ServerProtocol
        reactor.listenTCP(4321, factory)
        reactor.run()
    
    if __name__ == '__main__':
        main()
    

    Notice how:

    • I got rid of the __init__ method on ClientProtocol. ClientFactory calls its protocol with no arguments. An __init__ that requires an argument will result in a TypeError being raised. Additionally, ClientFactory already sets itself as the factory attribute of protocols it creates.
    • I gave ClientProtocol a reference to the ServerProtocol instance by setting the ServerProtocol instance as the originator attribute on the client factory. Since the ClientProtocol instance has a reference to the ClientFactory instance, that means it has a reference to the ServerProtocol instance.
    • I added the forwardLine method which ClientProtocol can use to direct ServerProtocol to do whatever your application logic is, once the ClientProtocol connection is established. Notice that because of the previous point, ClientProtocol has no problem calling this method.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given this code snippet that can be readily pasted into Linqpad (or slightly modified
I have a slightly modified AccountController that should write a row into a table
I have this page in which I slightly modified the jQuery autocomplete to make
We have a custom site written in Perl that uses a slightly modified version
I slightly remember that there is an class which is capable of stretching an
I am using a slightly modified version of this code . to create a
I'm using slightly modified sample code provided by the YUI team. When my source
Below is my VHost (which is slightly modified to obscure some URLS): 1 NameVirtualHost
I believe that running an external command with a slightly modified environment is a
I am using a slightly modified version of the DotZlib which is part 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.