Working with sockets in python 2.7, and I’ve encountered a Type error as follows:
TypeError: coercing to Unicode: need string or buffer, instance found.
I’m sure this has something to do with the differences between python2 and python3 in terms of unicode. But I’m not familiar enough with the differences to narrow down how to fix the problem. I’m sure I know where the problem is, but I google has failed to assist me in finding a solution. Here’s the code segment I’m having problems with.
from IPy import IP
ip = IP(sys.argv[1])
for x in ip:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
svr = (x, 25)
s.connect(svr)
data = s.recv(2048)
s.close()
print(repr(data))
Here is the exact printout of the error:
Traceback (most recent call last):
File "relayscanner.py", line 17, in <module>
s.connect(tgt)
File "C:\Python27\lib\socket.py", line 222, in meth
return getattr(self._sock,name)(*args)
TypeError: coercing to Unicode: need string or buffer, instance found
I think the problem is that you are giving
socket.connect()anIPy.IPobject, but it expects a string. Try coercing it to a string first, IE: