Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #57667 > unrolled thread
| Started by | theelder777@gmail.com |
|---|---|
| First post | 2013-10-26 14:41 -0700 |
| Last post | 2013-10-27 01:53 -0700 |
| Articles | 16 — 7 participants |
Back to article view | Back to comp.lang.python
array/list of sockets theelder777@gmail.com - 2013-10-26 14:41 -0700
Re: array/list of sockets Mark Lawrence <breamoreboy@yahoo.co.uk> - 2013-10-26 23:01 +0100
Re: array/list of sockets Johannes Findeisen <mailman@hanez.org> - 2013-10-26 23:59 +0200
Re: array/list of sockets theelder777@gmail.com - 2013-10-26 15:15 -0700
Re: array/list of sockets Chris Angelico <rosuav@gmail.com> - 2013-10-27 09:31 +1100
Re: array/list of sockets Mark Lawrence <breamoreboy@yahoo.co.uk> - 2013-10-26 23:33 +0100
Re: array/list of sockets theelder777@gmail.com - 2013-10-26 15:42 -0700
Re: array/list of sockets Mark Lawrence <breamoreboy@yahoo.co.uk> - 2013-10-27 00:03 +0100
Re: array/list of sockets theelder777@gmail.com - 2013-10-26 16:17 -0700
Re: array/list of sockets mm0fmf <none@mailinator.com> - 2013-10-27 00:26 +0100
Re: array/list of sockets Chris Angelico <rosuav@gmail.com> - 2013-10-27 10:27 +1100
Re: array/list of sockets Mark Lawrence <breamoreboy@yahoo.co.uk> - 2013-10-27 00:36 +0100
Re: array/list of sockets theelder777@gmail.com - 2013-10-26 16:56 -0700
Re: array/list of sockets Chris Angelico <rosuav@gmail.com> - 2013-10-27 11:05 +1100
Re: array/list of sockets rurpy@yahoo.com - 2013-10-26 21:29 -0700
Re: array/list of sockets rusi <rustompmody@gmail.com> - 2013-10-27 01:53 -0700
| From | theelder777@gmail.com |
|---|---|
| Date | 2013-10-26 14:41 -0700 |
| Subject | array/list of sockets |
| Message-ID | <f31c18ac-ef3b-42b9-90cd-c0ef4bd80c06@googlegroups.com> |
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[i], addr = serverSocket.accept()
and
message = myCSocks[i].recv(1024)
I am getting the following error:
Traceback (most recent call last):
File "./htmlserv_multi.py", line 22, in <module>
message = myCSocks[i].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.
[toc] | [next] | [standalone]
| From | Mark Lawrence <breamoreboy@yahoo.co.uk> |
|---|---|
| Date | 2013-10-26 23:01 +0100 |
| Message-ID | <mailman.1616.1382824883.18130.python-list@python.org> |
| In reply to | #57667 |
On 26/10/2013 22:41, theelder777@gmail.com wrote: > 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'] This is a list of Python strings, not an array, not sockets. > > and I am trying to use my array elements as follows: > myCSocks[i], addr = serverSocket.accept() > and > message = myCSocks[i].recv(1024) > > I am getting the following error: > Traceback (most recent call last): > File "./htmlserv_multi.py", line 22, in <module> > message = myCSocks[i].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. > See this http://docs.python.org/3/library/socket.html#example -- Python is the second best programming language in the world. But the best has yet to be invented. Christian Tismer Mark Lawrence
[toc] | [prev] | [next] | [standalone]
| From | Johannes Findeisen <mailman@hanez.org> |
|---|---|
| Date | 2013-10-26 23:59 +0200 |
| Message-ID | <mailman.1617.1382825211.18130.python-list@python.org> |
| In reply to | #57667 |
Hi, On Sat, 26 Oct 2013 14:41:15 -0700 (PDT) theelder777@ wrote: > 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
[toc] | [prev] | [next] | [standalone]
| From | theelder777@gmail.com |
|---|---|
| Date | 2013-10-26 15:15 -0700 |
| Message-ID | <bd64d4e7-6f99-4c59-b78f-7253f2240147@googlegroups.com> |
| In reply to | #57667 |
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?
[toc] | [prev] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2013-10-27 09:31 +1100 |
| Message-ID | <mailman.1620.1382826677.18130.python-list@python.org> |
| In reply to | #57674 |
On Sun, Oct 27, 2013 at 9:15 AM, <theelder777@gmail.com> wrote:
> 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 (python-list@python.org).
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
[toc] | [prev] | [next] | [standalone]
| From | Mark Lawrence <breamoreboy@yahoo.co.uk> |
|---|---|
| Date | 2013-10-26 23:33 +0100 |
| Message-ID | <mailman.1621.1382826844.18130.python-list@python.org> |
| In reply to | #57674 |
On 26/10/2013 23:15, theelder777@gmail.com wrote: > 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? > It's a list :) And if it works for you, why not, there's nothing at all wrong with KISS? -- Python is the second best programming language in the world. But the best has yet to be invented. Christian Tismer Mark Lawrence
[toc] | [prev] | [next] | [standalone]
| From | theelder777@gmail.com |
|---|---|
| Date | 2013-10-26 15:42 -0700 |
| Message-ID | <a41e5b28-c366-4a57-a04f-be93e991b2e0@googlegroups.com> |
| In reply to | #57667 |
Thank you all for your time and great replies. I think/hope I have enough info to able to implement this simple server.
[toc] | [prev] | [next] | [standalone]
| From | Mark Lawrence <breamoreboy@yahoo.co.uk> |
|---|---|
| Date | 2013-10-27 00:03 +0100 |
| Message-ID | <mailman.1622.1382828644.18130.python-list@python.org> |
| In reply to | #57679 |
On 26/10/2013 23:42, theelder777@gmail.com wrote: > 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 :) -- Python is the second best programming language in the world. But the best has yet to be invented. Christian Tismer Mark Lawrence
[toc] | [prev] | [next] | [standalone]
| From | theelder777@gmail.com |
|---|---|
| Date | 2013-10-26 16:17 -0700 |
| Message-ID | <579a6b22-7660-4aed-b709-480a6221eeeb@googlegroups.com> |
| In reply to | #57680 |
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 : )
[toc] | [prev] | [next] | [standalone]
| From | mm0fmf <none@mailinator.com> |
|---|---|
| Date | 2013-10-27 00:26 +0100 |
| Message-ID | <BcYau.21507$JS6.17046@fx31.am4> |
| In reply to | #57681 |
On 27/10/2013 00:17, theelder777@gmail.com wrote: > > 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
[toc] | [prev] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2013-10-27 10:27 +1100 |
| Message-ID | <mailman.1623.1382830069.18130.python-list@python.org> |
| In reply to | #57681 |
On Sun, Oct 27, 2013 at 10:17 AM, <theelder777@gmail.com> wrote: > > 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 : ) 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 python-list@python.org by going to https://mail.python.org/mailman/listinfo/python-list - it's the same content, viewed as emails. ChrisA
[toc] | [prev] | [next] | [standalone]
| From | Mark Lawrence <breamoreboy@yahoo.co.uk> |
|---|---|
| Date | 2013-10-27 00:36 +0100 |
| Message-ID | <mailman.1624.1382830597.18130.python-list@python.org> |
| In reply to | #57681 |
On 27/10/2013 00:17, theelder777@gmail.com wrote: > > 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? -- Python is the second best programming language in the world. But the best has yet to be invented. Christian Tismer Mark Lawrence
[toc] | [prev] | [next] | [standalone]
| From | theelder777@gmail.com |
|---|---|
| Date | 2013-10-26 16:56 -0700 |
| Message-ID | <08c59ea9-2639-41aa-a9c8-7da16842a866@googlegroups.com> |
| In reply to | #57685 |
On Saturday, October 26, 2013 4:36:04 PM UTC-7, Mark Lawrence wrote: > 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.
[toc] | [prev] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2013-10-27 11:05 +1100 |
| Message-ID | <mailman.1625.1382832314.18130.python-list@python.org> |
| In reply to | #57687 |
On Sun, Oct 27, 2013 at 10:56 AM, <theelder777@gmail.com> wrote: > 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 python-list@python.org); everything else should work. ChrisA
[toc] | [prev] | [next] | [standalone]
| From | rurpy@yahoo.com |
|---|---|
| Date | 2013-10-26 21:29 -0700 |
| Message-ID | <648bdfb6-93d8-46a5-8e94-ccbdc9a7f5dd@googlegroups.com> |
| In reply to | #57681 |
On 10/26/2013 05:17 PM, theelder777@gmail.com wrote: > 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.
[toc] | [prev] | [next] | [standalone]
| From | rusi <rustompmody@gmail.com> |
|---|---|
| Date | 2013-10-27 01:53 -0700 |
| Message-ID | <0055a443-8885-4240-87ba-b1791d572932@googlegroups.com> |
| In reply to | #57681 |
On Sunday, October 27, 2013 4:47:16 AM UTC+5:30, theel...@gmail.com wrote: > 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
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web