python3: why writing to socket require bytes type while writing to a file require str ?

Yubin Ruan ablacktshirt at gmail.com
Fri Jul 22 04:52:18 EDT 2016


Hi,
    I'm migrating my code to python3 now and find it hard to deal with python's 'str' and 'bytes' type. It's kind of painful.
    One thing I find really confusing is that, writing to a socket requires argument to be of type 'bytes'(otherwise python throw 'str does not support buffer interface...' exception), while writing to a file requires argument to be of type 'str'. Why is that? In standard UNIX interface, everything is file, which mean that writing to a socket is the same as writing to a normal file. This is true is python2. But thing doesn't seem to work in python 3.
    Does anyone have idea why is this in python3 ?
    In my understanding, writing to a file would requires that everything be written byte by byte. So writing objects of type 'bytes' to socket makes sense. But, how could python write to file using unicode(type 'str')? Does python encode 'str'(Unicode) automatically before writing things to file? If it's true, then why can't it do that automatic encoding when I trying to write a 'str' to socket ?

Regards,
Ruan



More information about the Python-list mailing list