array/list of sockets

T

theelder777

Hello all, I am kind of new to python. I am currently trying to make and use a list/array of sockets in a program. So I have declared an array as follows:
myCSocks = ['CSock1', 'CSock2', 'CSock3', 'CSock4', 'CSock5']

and I am trying to use my array elements as follows:
myCSocks, addr = serverSocket.accept()
and
message = myCSocks.recv(1024)

I am getting the following error:
Traceback (most recent call last):
File "./htmlserv_multi.py", line 22, in <module>
message = myCSocks.recv(1024)
AttributeError: 'str' object has no attribute 'recv'


This kind of makes sense to me, it is saying that my array elements are of type String and are not sockets. So I understand what my problem is but I do not know how to remedy it. I have googled "list of sockets python" but did not find anything. Any help will be greatly appreciated. Thank you.
 
J

Johannes Findeisen

Hi,

Hello all, I am kind of new to python. I am currently trying to make and use a list/array of sockets in a program. So I have declared an array as follows:
myCSocks = ['CSock1', 'CSock2', 'CSock3', 'CSock4', 'CSock5']

You actually have added strings to your list. this 'is_a_string.

You need to add the objcts to the list. E.g.:

myCSocks = [CSock1, CSock2, CSock3, CSock4, CSock5]

Hope this helps.

Regards,
Johannes
 
T

theelder777

Thank you for your quick replies. I am trying to implement a very simple multithreaded webserver in python. Is using an array of sockets (for different clients) the correct way to go?
 
C

Chris Angelico

Thank you for your quick replies. I am trying to implement a very simple multithreaded webserver in python. Is using an array of sockets (for different clients) the correct way to go?

Firstly, before you respond any more, please get off Google Groups.
You are omitting context, which makes threading (topic threading here,
distinct from threads of execution) difficult to follow. Get a news
reader like Thunderbird, or switch to email ([email protected]).
It will be greatly appreciated. :)

As to threading in Python... You don't normally need to maintain a
list of sockets; the easiest way would be to spin off a thread every
time a new connection happens. In pseudo-code:

def connection_thread(sock):
while True:
sock.recv(...)
do stuff with what you got, maybe write to the socket

def acceptloop():
sock = ... however you're creating your listening socket
while True:
newsock = sock.accept()
create new thread, connection_thread, newsock

Each thread needs only care about its one socket.

Now, if you're creating a chat server, where anything that comes in on
one socket goes to all the others, then you'll need to maintain that
list (that's why I said "normally"). In that case, the easiest way
would probably be for connection_thread to register its socket when it
begins, and unregister it when it ends (using try/finally to help).

There are other ways to manage multiple sockets. You could maintain a
list of them all and react any time one is readable/writable, with
select(); I'm not sure how to do that in Python as I've never done so,
but there's a way. With that, you'd simply add sockets to the list
when accept() has something, and remove them when they're closed. But
you said you were threading.

ChrisA
 
T

theelder777

Thank you all for your time and great replies. I think/hope I have enough info to able to implement this simple server.
 
M

Mark Lawrence

Thank you all for your time and great replies. I think/hope I have enough info to able to implement this simple server.

No problem but please take onboard the advice Chris Angelico gave you
earlier regarding google groups, noting the complete lack of any context
with your words above :)
 
T

theelder777

I apologize but I do not understand what you mean by "lack of context." I have taken Chris' words into consideration, for my previous post was supposed to be my last (I just had to say thank you). This is my first google groups post, so I am a total noob. Once again, I apologize for whatever that I may have done. I will not post in google groups again : )
 
M

mm0fmf

I apologize but I do not understand what you mean by "lack of context." I have taken Chris' words into consideration, for my previous post was supposed to be my last (I just had to say thank you). This is my first google groups post, so I am a total noob. Once again, I apologize for whatever that I may have done. I will not post in google groups again : )

What is meant by lack of context is that your post only contains your
words. It does not contain any words from the message(s) you are
replying to. i.e. there is no context for your reply.

If you look at this message you'll see a line saying who said the quoted
text followed by the quoted text. Then you see my reply. With that
context, hopefully, my reply makes sense. Someone should be able to
understand what the reply means as they have the context for the reply
all in this message.

Andy
 
C

Chris Angelico

I apologize but I do not understand what you mean by "lack of context." Ihave taken Chris' words into consideration, for my previous post was supposed to be my last (I just had to say thank you). This is my first google groups post, so I am a total noob. Once again, I apologize for whatever that I may have done. I will not post in google groups again : )

It's conventional, on newsgroups and mailing lists, to quote a piece
of the previous post to show what you're replying to. That's called
context. Google Groups makes this difficult and ugly, which is why we
recommend not using it. There are other interfaces to the same
newsgroup, which do not have this problem; Thunderbird is a
well-recommended one, and you can also subscribe to
(e-mail address removed) by going to
https://mail.python.org/mailman/listinfo/python-list - it's the same
content, viewed as emails.

ChrisA
 
M

Mark Lawrence

I apologize but I do not understand what you mean by "lack of context." I have taken Chris' words into consideration, for my previous post was supposed to be my last (I just had to say thank you). This is my first google groups post, so I am a total noob. Once again, I apologize for whatever that I may have done. I will not post in google groups again : )

That's okay, everybody has to start somewhere :)

A definition from http://www.thefreedictionary.com/context "The part of
a text or statement that surrounds a particular word or passage and
determines its meaning". So lack of context means the surrounding part
is missing, which applies to your words above as nobody knows at a
glance to whom or to what you're replying. Clear I hope?
 
T

theelder777

That's okay, everybody has to start somewhere :)



A definition from http://www.thefreedictionary.com/context "The part of

a text or statement that surrounds a particular word or passage and

determines its meaning". So lack of context means the surrounding part

is missing, which applies to your words above as nobody knows at a

glance to whom or to what you're replying. Clear I hope?



--

Python is the second best programming language in the world.

But the best has yet to be invented. Christian Tismer



Mark Lawrence

Yes, crystal clear. Thank you. Since I am writing this post, I have one final question. I got my code to work for a multithreaded web server, how do I test if it can handle multiple threads?

Also as a side note, I have subscribed to the python mailing list but have no idea how to use it. Thank you everyone for being patient.
 
C

Chris Angelico

Yes, crystal clear. Thank you. Since I am writing this post, I have one final question. I got my code to work for a multithreaded web server, how do I test if it can handle multiple threads?

Easy! Just make sure the threads take a good bit of time - add calls
to time.sleep() if necessary - and then start multiple clients at the
same time.
Also as a side note, I have subscribed to the python mailing list but have no idea how to use it. Thank you everyone for being patient.

You should see this post of mine in your emails. When you do, just
reply (and make sure you're sending to (e-mail address removed));
everything else should work.

ChrisA
 
R

rurpy

I apologize but I do not understand what you mean by "lack of
context." I have taken Chris' words into consideration, for my
previous post was supposed to be my last (I just had to say thank
you). This is my first google groups post, so I am a total noob. Once
again, I apologize for whatever that I may have done. I will not post
in google groups again : )

Please be aware that Chris and a handful of other people
here are running a crusade against Google Groups because
they don't like the format of posts from there or for
other reasons.

Chris (& Co.) frequently misrepresent themselves as
speaking for the entire community when in fact that is
not true. Many people here post via Google Groups such
as myself and have been doing so for years. So please
feel free to ignore his request if Google Groups is
convenient for you.

You can also look at
https://wiki.python.org/moin/GoogleGroupsPython
for some suggestions on how to make GG posts less irksome
to people who are hyper-sensitive to formatting.

BTW, "context" is the text I included above prefixed with
">" that is from your post that I'm responding to. It
serves to remind the reader what the discussion is about.
 
R

rusi

I apologize but I do not understand what you mean by "lack of context." I have
taken Chris' words into consideration, for my previous post was supposed to be
my last (I just had to say thank you). This is my first google groups post, so > I am a total noob. Once again, I apologize for whatever that I may have done. I
will not post in google groups again : )

See http://en.wikipedia.org/wiki/Posting_style
in particular the difference between 'top-posting' and 'interleaved style'

There is no universal standard about this.
Ive been in corporate cultures where doing anything other than top-posting is taken as doing something fishy -- hiding, distorting etc.

However on this forum which BTW is Usenet and predates the popular internet (WWW) by some decades, it is considered impolite to do anything other than 'interleaved'

This means
1. You need to cut out most of useless crufty context
2. Keep a little so that the reader knows who/what you are speaking in response to
3. Keep your response as close to and generally below the context
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,058
Latest member
QQXCharlot

Latest Threads

Top