0
>>> pkt = sniff(count=2,filter="tcp")
>>> raw  = pkt[1].sprintf('%Padding.load%')
>>> raw
"'\\x04\\x00\\x00\\x00\\x00\\x00\\x00\\x00g\\xc4|\\x00\\x00\\x00\\x00\\x00'"


>>> print raw
'\x04\x00\x00\x00\x00\x00\x00\x00g\xc4|\x00\x00\x00\x00\x00'

Raw yield different output when use print

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Avihai Marchiano
  • 3,837
  • 3
  • 38
  • 55

2 Answers2

5

One is the repr() representation of the string, the other the printed string. The representation you can paste back into the interpreter to make the same string again.

The Python interactive prompt always uses repr() when echoing variables, print always uses the str() string representation.

They are otherwise the same. Try print repr(raw) for comparison:

>>> "'\\x04\\x00\\x00\\x00\\x00\\x00\\x00\\x00g\\xc4|\\x00\\x00\\x00\\x00\\x00'"
"'\\x04\\x00\\x00\\x00\\x00\\x00\\x00\\x00g\\xc4|\\x00\\x00\\x00\\x00\\x00'"
>>> print "'\\x04\\x00\\x00\\x00\\x00\\x00\\x00\\x00g\\xc4|\\x00\\x00\\x00\\x00\\x00'"
'\x04\x00\x00\x00\x00\x00\x00\x00g\xc4|\x00\x00\x00\x00\x00'
>>> print repr("'\\x04\\x00\\x00\\x00\\x00\\x00\\x00\\x00g\\xc4|\\x00\\x00\\x00\\x00\\x00'")
"'\\x04\\x00\\x00\\x00\\x00\\x00\\x00\\x00g\\xc4|\\x00\\x00\\x00\\x00\\x00'"
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
1

__str__ and __repr__ built in methods of a class can return whatever string values they want. Some classes will simply use a str() for their repr.

class AClass(object):

   def __str__(self):
      return "aclass"

   def __repr__(self):
      return str(self)

class AClass2(AClass):

   def __repr__(self):
      return "<something else>"

In [2]: aclass = AC
AClass   AClass2  

In [2]: aclass = AClass()

In [3]: print aclass
aclass

In [4]: aclass
Out[4]: aclass

In [5]: aclass2 = AClass2()

In [6]: print aclass2
aclass

In [7]: aclass2
Out[7]: <something else>

In [8]: repr(aclass2)
Out[8]: '<something else>'

In [9]: repr(aclass)
Out[9]: 'aclass'

repr is simply meant to show a "label" of the class, such as when you print a list that contains a bunch of this instance...how it should look.

str is how to convert the instance into a proper string value to be used in operations.

jdi
  • 90,542
  • 19
  • 167
  • 203