Use StringIO module to read string
Read string
import string# from ww w .j a va 2 s.c o m
contents = "<data><ref id='bit'><para>0</para><para>1</para></ref></data>"
import StringIO
ssock = StringIO.StringIO(contents)
print ssock.read()
print ssock.read()
print ssock.seek(0)
print ssock.read(15)
print ssock.read(15)
print ssock.read()
ssock.close()
The code above generates the following result.