0

Continuing from the question mentioned in the below link

list/array of sockets in python

Is it possible to create an array of sockets in python like

list=[socket0, socket1, socket2, socket3 ]

for i in range(0,3):
    list[i]=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    list[i].connect((host,port[0]))

I tried this but I'm getting the same error as I have posted in the link that no attribute recv.

Is it possible to create an array like this in python. I know it's possible in C.

2
  • don't you mean list[i]=socket.socket...? Commented Dec 7, 2015 at 6:39
  • 1
    You can create a list containing any object you like, hell even classes (which are objects too in python). Start with an empty list and add your instantiated socket objects. Judging by your questions however, I get the feeling that you should start by understanding simple programming concepts first. Commented Dec 7, 2015 at 6:44

2 Answers 2

2

You should not pre-populate your list, but create it on the fly.

There are two way how you can do that:

  1. The "better" way:

    sockets = [socket.socket(socket.AF_INET, socket.SOCK_STREAM) for _ in range(3)]
    for sock in sockets:
         sock.connect((host, port[0]))
    
  2. The inferior way:

    sockets = []
    for i in range(3):
        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        sock.connect((host, port[0]))
        sockets.append(sock)
    

Despite the extra iteration, the first one is better because it uses one of Pythons "best" festures for constructing a list and is shorter and more readable. The extra iteration's timing requirements is low to non-existent.

However, there is nothing which is really against the second one, so if you prefer it although it is longer, use that.

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

4 Comments

Can you give a reasoning for why the list comprehension way is better? Now you are iterating twice. (Dont get me wrong I like the idea and what it does for readability, but OP does not seem to be very experienced, so maybe explain what makes it superior)
@enpenax both options have connect call. It is a network IO operation and I suspect it is going to last enough so we can say that the other code took a negligible amount of time to execute no matter how many loops are there. So it's all about readability.
@enpenax The mere iterating doesn't cost so much that it would outweigh the readability. Note that it is about 4 list entries...
As I said, don't get me wrong. I fully agree, but it is easy to write "this solution is superior" without giving the reasoning in the post :) Now I can upvote this :P but maybe it will never be marked as accepted :/
0

The fastest way:

import socket
sockets = list(map(lambda x: x.connect(('127.0.0.1', 80)), [socket.socket(socket.AF_INET, socket.SOCK_STREAM) for _ in range(3)]))

List comprehension works faster than creation of loop with continuous append or list[i], map function does too.

More beautiful way(slightly less efficient):

import socket

not_connected_sockets = [socket.socket(socket.AF_INET, socket.SOCK_STREAM) for _ in
    range(3)]

sockets = list(map(lambda x: x.connect(('127.0.0.1',
    80)), not_connected_sockets))

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.