1

So I am trying to pull some IP's from a text file and using that list in the socket connection. My issue is I keep getting the following error:

TypeError: str, bytes or bytearray expected, not list

Here is the code im using:

    import socket

     ips = open('list.txt', 'r').readlines()

     def displayType(sec_type):
       switcher = {
        0: "1",
        1: "2",
        2: "3"
       } 
       print(' type: ' + str(sec_type) + ' (' + switcher.get(sec_type,"Not defined by IETF") +')' )

    try:
       def check(ip,port):
         link = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
         link.connect((ip,int(port)))
         link_ver = link.recv(12)
         print(link_ver)

         link.send(link_ver)
         nb_sec_types = ord(link.recv(1))
         print("types: " + str(nb_sec_types))

    check(ips,"80")

If anyone has an idea on how to use the ip's from the list that would be great.

4
  • The error is pretty intuitive.. which line raises the error? Commented Apr 21, 2018 at 13:22
  • sorry about that. its the line that has: check(ips,"5900") & also : link.connect((ip,int(port))) Commented Apr 21, 2018 at 13:24
  • the object ips Is a list. When you call link.connect, you end up passing a list as an argument, but it says that link.connect expects a string instead. You can fix that by iterating through your list of ips, for example: for ip in ips: check(ip, "80) Commented Apr 21, 2018 at 13:28
  • Awesome thank you for that I made the changes and it is working. Commented Apr 21, 2018 at 13:33

3 Answers 3

1

Change

check(ips, "80) 

for

for ip in ips: 
    check(ip, "80) 

link.connect expected a unique ip and you're passing a list of ips.

Sign up to request clarification or add additional context in comments.

Comments

0

The method readlines() reads until EOF and returns a list containing the lines. So you are getting a list in ips. You will need to iterate over this list and call check() on each iteration.

Also, you have used try block without except or finally. This will result into error.

You can put catch() inside the try-except block as:

try:
 catch(ip, port)
except Exception as e:
 print(str(e))

Comments

0

You must first convert the string that contains the IP address into a byte or a string of bytes and then start communicating. According to the code below, your error will be resolved. Make sure your code is working correctly overall.

string = '192.168.1.102'
new_string = bytearray(string,"ascii")
ip_receiver = new_string
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.sendto(text.encode(), (ip_receiver, 5252))

Comments

Your Answer

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.