-1

What is the maximum number of characters that can be stored in a string variable in Python? I need to assign the whole configuration of a router to a variable, but the configuration is huge. Is any other type of variable I can use? This is part of my code:

        self.output = self.channel.recv(64512)
        while self.channel.recv_ready():
            time.sleep(1)
            print("Waiting for channel ready",count)
            self.output += self.channel.recv(64512)
            count -= 1
            if  count == 0:
                break

This code receive info from the router and stores it in self.output variable. After exactly received the same amount of info, this code arrises and Exception. Help please. Thank you. Osmany

Osmany
  • 7
  • 1
  • 2
    Which Exception are you getting? – ywbaek Mar 10 '20 at 23:09
  • If you think you're hitting a string size limit, what you're probably hitting is something else. Maybe misunderstanding how `self.channel.recv` handles the size argument. – user2357112 Mar 10 '20 at 23:10
  • Probably a [duplicate question][1], and the answer indicate that you should be good. Have you considered writing to a file? [1]: https://stackoverflow.com/questions/1739913/what-is-the-max-length-of-a-python-string – inverzeio Mar 10 '20 at 23:10
  • What is `self.channel`? Is it a paramiko ssh channel? If you are on a regular PC, a router configuration is trivially small compared to RAM and a single string is fine. You were asked to post the actual error. You should get that done while people are still interested in the question. Why make us wait? – tdelaney Mar 10 '20 at 23:21
  • 1
    What is `count` for? You only want to grab data a certain number of times... but why? – tdelaney Mar 10 '20 at 23:24
  • @inverzeio - I agree that's a good answer to the question, but I think there is a completely different bug here unrelated to string size. So lets not mark it a dup until OP gives more data. – tdelaney Mar 10 '20 at 23:26
  • Thanks everyone, II use Paramiko for connecting to routers. I used to send commands to the router and receive the reply that I store in a string variable. The problem is, that sometimes the information received from the routers are huge and the length of the string variable increase enormously. My problem was a counter of seconds, that waits for the channel.recv_redy to change its state to False, it was too short, so the loop that receives the data is interrupted and the Exception comes out. It's not a problem of the string size. Thanks 2 all for answers – Osmany Mar 11 '20 at 18:57

1 Answers1

0

The limit, that you encounter is not the limit introduced by the max string length, this one will be in the gigabyte range.

What you probably encounter is a limit of the ssl library or of some TCP socket libraries.

gelonida
  • 5,327
  • 2
  • 23
  • 41
  • Thanks, for your answer gelonida, in fact, I've realized that the problem was, not waiting enough time for the channel's buffer to get empty, not the variable size. I've noticed a string variable can store at least Mbytes, which is fine for me. thank you again. – Osmany Mar 11 '20 at 11:21