1
import socket
from datetime import datetime
#User input
ServerIP = raw_input("Enter server IP: ")
RemoteServerIP = socket.gethostbyaddr(ServerIP)
print "-" * 60
print "Please wait, scanning remote host", RemoteServerIP
print "-" * 60
#Starting to scan
StartTime = datetime.now()
for port in range(1,1025):#ports 1-1024
 sock = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
 result = sock.connect_ex((RemoteServerIP, port))
 if result == 0:
 print "Port {}: \t Open".format(port)
sock.close()
#caculate the time and print to screen
EndTime = datetime.now()
TotalTime = StartTime - EndTime 
print 'Scanning Completed in: ', TotalTime

Error :

 result = sock.connect_ex((RemoteServerIP, port))
File "C:\Python27\lib\socket.py", line 228, in meth
 return getattr(self._sock,name)(*args)
TypeError: coercing to Unicode: need string or buffer, tuple found
Perry
3,90326 gold badges40 silver badges51 bronze badges
asked Nov 14, 2015 at 13:42

1 Answer 1

1

socket.gethostbyaddr() returns a tuple. Accessing the first item in the ipaddrlist should get you what you need:

RemoteServerIP = socket.gethostbyaddr(ServerIP)[2][0]
answered Nov 14, 2015 at 13:52
Sign up to request clarification or add additional context in comments.

2 Comments

ty very much for helping me
My pleasure! Best of luck to you.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.