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

  • Home
  • SEARCH
  • 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 6185501
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T01:43:27+00:00 2026-05-24T01:43:27+00:00

I am trying to create a simple voucher program. Client connects to server and

  • 0

I am trying to create a simple voucher program.

Client connects to server and asks if a voucher has time left on it, if yes the server responds with how much time.

I have control of the server and the clients, the client side is coded by me as well.

Right now this is what I have for my server side, the client side is self explanatory.

The big flaw in this, is that if 2 clients connect with the same voucher code, they will both have access because the server is not checking if there is an active client with that code.

Could anyone explain or lead to documentation in how this is possible ?

#!/usr/bin/env python

from twisted.internet import reactor, protocol

class Responder(protocol.Protocol):

    def dataReceived(self, data):
        # check the voucher code, and return disabled if its out of time or not there. Otherwise return time left.
        if data.startswith("check="):
            param, vcode = data.split("=")
            checkcode = SQLConnect("check", vcode, vcode)
            if checkcode == "disabled":
                self.transport.write("disabled")
            else:
                self.transport.write(str(checkcode))
        # Update time left.
        if data.startswith("update="):
            param, vcode, vtime = data.split("=")
            SQLConnect("update", vcode, vtime)

def main():
    factory = protocol.ServerFactory()
    factory.protocol = Responder
    reactor.listenTCP(6500,factory)
    reactor.run()

if __name__ == '__main__':
    main()
  • 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-05-24T01:43:27+00:00Added an answer on May 24, 2026 at 1:43 am

    If a voucher becomes “in use” when a client checks it and then becomes unused when the client disconnects, it sounds like you just need to keep a set of vouchers which you add to when the check is done and remove from when the client disconnects. You could keep this on the factory so it is shared between all client connections. For example:

    #!/usr/bin/env python
    
    from twisted.internet import reactor, protocol
    
    class Responder(protocol.Protocol):
        def connectionMade(self):
            self.vcode = None
    
        def dataReceived(self, data):
            # check the voucher code, and return disabled if its out of time or not there. Otherwise return time left.
            if data.startswith("check="):
                param, vcode = data.split("=")
                if vcode in self.factory.activeVouchers:
                    self.transport.write("in use")
                    return
                self.factory.activeVouchers.add(vcode)
                self.vcode = vcode
    
                checkcode = SQLConnect("check", vcode, vcode)
                if checkcode == "disabled":
                    self.transport.write("disabled")
                else:
                    self.transport.write(str(checkcode))
            # Update time left.
            if data.startswith("update="):
                param, vcode, vtime = data.split("=")
                SQLConnect("update", vcode, vtime)
    
        def connectionLost(self, reason):
            if self.vcode is not None:
                self.factory.activeVouchers.remove(self.vcode)
    
    def main():
        factory = protocol.ServerFactory()
        factory.activeVouchers = set()
        factory.protocol = Responder
        reactor.listenTCP(6500,factory)
        reactor.run()
    
    if __name__ == '__main__':
        main()
    

    The new vcode attribute on Responder lets the activeVouchers set get updated when the client disconnects (which triggers the Responder.connectionLost call). The additional check near the beginning of Responder.dataReceived adds vouchers to that set when they become used and prevents any in use voucher from being claimed.

    Apart from that, there are a couple other things you might want to consider. First, you should probably use twisted.protocols.basic.LineOnlyReceiver or one of the other protocols in twisted.protocols.basic instead of just Protocol. dataReceived is called whenever any bytes are received over the network. Since TCP is a stream-oriented transport rather than a message-oriented transported, you may end up with a call like dataReceived("chec") quickly followed by dataReceived("k=somecode"). As your dataReceived is implemented now, this case won’t be handled and the client will receive no response. LineOnlyReceiver adds line-based framing, so that bytes like “check=somecode\r\n” can be interpreted as a complete message and “check=somecode” delivered all at once, in a single lineReceived call.

    Second, SQLConnect looks like it probably performs some blocking I/O. If this is true, it means that your server won’t handle concurrent client requests very well, since any blocking prevents all new events from being handled, including those from different clients. You might want to take a look at twisted.enterprise.adbapi for a non-blocking SQL API.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to create a simple dialog in MFC using Visual C++. My
I am trying to create a simple page that enters data in to a
I'm trying to create a simple BaSH-like grammar on ANTLRv3 but haven't been able
I'm trying to create a simple toggling sidebar using jquery, where it expands and
I'm playing with the ASP.NET MVC Framework, trying to create a simple site. My
I am trying to create a rather simple effect on a set of images.
I'm new to postgreSQL and I have a simple question: I'm trying to create
Im using Eclipse 3.4, EclipseMe 1.7.9. Im trying to deploy/create package a simple project
I'm trying to come up with a simple and efficient way to create a
I am trying create a simple WQL query where I only return mounted drives

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.