Forums

Porting script from Python2 to Python3

Below is a script that I am trying to port from Python2 to Python3. However, I'm getting the error message below.

I believe it has something to do with the way Python3 handles string and bytes, but I can't work out what to do.

!/usr/bin/python

import socket import sys

s=socket.socket(socket.AF_INET, socket.SOCK_STREAM)

connect=s.connect(('192.168.17.215',25))

banner=s.recv(1024) print (banner)

usernames = open('/root/user-names.txt', 'r')

for line in iter(usernames): line=line.strip() print (line) s.send('VRFY ' + line + '\r\n') result=s.recv(1024) print (result)

usernames.close()

### Error is below

b'220 redhat.acme.com ESMTP Sendmail 8.12.8/8.12.8; Wed, 12 Feb 2014 22:07:12 +0200\r\n' root Traceback (most recent call last): File "vrfy_username_discovery_automated-P3.py", line 40, in <module> s.send('VRFY ' + line + '\r\n') TypeError: 'str' does not support the buffer interface

Check out 'encode' and 'decode' - basically the socket methods don't understand strings in 3.3.

3.3 separates 'strings' and 'bytes'.

Jim

+1 to that!