[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

pavloutefkros@gmail.com

3/2/2008 1:09:00 PM

i have this small script which after some router configurations works.

##########################################################

#! /usr/bin/python
import socket

HOST = ''
PORT = 1515
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((HOST, PORT))
s.listen(1)
conn, addr = s.accept()
conn.send('HTTP/1.1 200 OK\r\n')
conn.send('Content-Type: text/html\r\n')
conn.send('Server: test/1.0\r\n\r\n')
conn.send('<html><body>test</body></html>')
s.close()

##########################################################

as you see it listens to 1515 until a connection is established and
then it accepts it...
the problem is that when it accepts the connection it sends those
strings and exits, but then it exits the program. i want it to listen
to 1515 then accept a connection, send.. and then listen to the port
again and again until new connections are found.

i've been struggling with try..except,while and all kinds of loops but
always new erros pop up, or it overflows.
14 Answers

7stud --

3/2/2008 1:58:00 PM

0

On Mar 2, 6:09 am, Gif <pavloutefk...@gmail.com> wrote:
> i have this small script which after some router configurations works.
>
> ##########################################################
>
> #! /usr/bin/python
> import socket
>
> HOST = ''
> PORT = 1515
> s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
> s.bind((HOST, PORT))
> s.listen(1)
> conn, addr = s.accept()
> conn.send('HTTP/1.1 200 OK\r\n')
> conn.send('Content-Type: text/html\r\n')
> conn.send('Server: test/1.0\r\n\r\n')
> conn.send('<html><body>test</body></html>')
> s.close()
>
> ##########################################################
>
> as you see it listens to 1515 until a connection is established and
> then it accepts it...
> the problem is that when it accepts the connection it sends those
> strings and exits, but then it exits the program. i want it to listen
> to 1515 then accept a connection, send.. and then listen to the port
> again and again until new connections are found.
>
> i've been struggling with try..except,while and all kinds of loops but
> always new erros pop up, or it overflows.

while True:
conn, addr = s.accept()
...

Steve Holden

3/2/2008 2:32:00 PM

0

7stud wrote:
> On Mar 2, 6:09 am, Gif <pavloutefk...@gmail.com> wrote:
>> i have this small script which after some router configurations works.
>>
>> ##########################################################
>>
>> #! /usr/bin/python
>> import socket
>>
>> HOST = ''
>> PORT = 1515
>> s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
>> s.bind((HOST, PORT))
>> s.listen(1)
>> conn, addr = s.accept()
>> conn.send('HTTP/1.1 200 OK\r\n')
>> conn.send('Content-Type: text/html\r\n')
>> conn.send('Server: test/1.0\r\n\r\n')
>> conn.send('<html><body>test</body></html>')
>> s.close()
>>
>> ##########################################################
>>
>> as you see it listens to 1515 until a connection is established and
>> then it accepts it...
>> the problem is that when it accepts the connection it sends those
>> strings and exits, but then it exits the program. i want it to listen
>> to 1515 then accept a connection, send.. and then listen to the port
>> again and again until new connections are found.
>>
>> i've been struggling with try..except,while and all kinds of loops but
>> always new erros pop up, or it overflows.
>
> while True:
> conn, addr = s.accept()
> ...

And now you get to start asking all the interesting questions that come
up, like "How do I get my server to respond to multiple requests in
parallel?"

It's a long road, but it's fun.

regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC http://www.hold...

pavloutefkros@gmail.com

3/2/2008 2:39:00 PM

0

you could at least check before posting. as i said i've tried like
1000 ways of doing that, and im so desparate that i'm thinking of
quiting python. This damn thing just doesnt work. when i do as you
post the server never even replies, as it tends to accept connections
all the time.

anyone has a better idea?

Diez B. Roggisch

3/2/2008 2:54:00 PM

0

Gif schrieb:
> you could at least check before posting. as i said i've tried like
> 1000 ways of doing that, and im so desparate that i'm thinking of
> quiting python. This damn thing just doesnt work. when i do as you
> post the server never even replies, as it tends to accept connections
> all the time.
>
> anyone has a better idea?

http://www.catb.org/~esr/faqs/smart-ques...

Nobody here knows what you did - post code & stacktraces of at least one
of your "1000 ways". Or quit python and try a language that is more
friendly to reading your mind instead requiring you to spell out things
in a computer readable way. You might need to hibernate a couple of
centuries though until it's sufficient to open notepad and write "I'm a
ubercool programmer, do make me the application of my dreams".

DIEZ

pavloutefkros@gmail.com

3/2/2008 2:55:00 PM

0

sorry for acting like a fool but this is just to weirdly easy that i
can't get to work. i've written a small web server in another language
and this is more like copying code.
i already have everything figured out, except this one but noone seems
either willing or capable of helping me.
again sorry but i was in a very bad mood.

pavloutefkros@gmail.com

3/2/2008 3:16:00 PM

0

i would like to apologize once more. i understand that you are saying
"what a fool he is waiting for us to solve all his problems", cause
i've said that for other posts, when they seemed "immature". It's just
that i couldn't find a way out of 20 lines of code and this drove me
mad.

i end this topic here.

Grant Edwards

3/2/2008 3:21:00 PM

0

On 2008-03-02, Gif <pavloutefkros@gmail.com> wrote:

> sorry for acting like a fool but this is just to weirdly easy
> that i can't get to work. i've written a small web server in
> another language and this is more like copying code. i already
> have everything figured out, except this one but noone seems
> either willing or capable of helping me.

Because you don't seem either willing or capable of describing
your problem in enough detail to allow anybody to help you.

Post a small program that demonstrates the "problem".

Describe precisely how that program fails to do what you want
it to.

--
Grant Edwards grante Yow! I was giving HAIR
at CUTS to th' SAUCER PEOPLE
visi.com ... I'm CLEAN!!

Diez B. Roggisch

3/2/2008 3:56:00 PM

0

Gif schrieb:
> sorry for acting like a fool but this is just to weirdly easy that i
> can't get to work. i've written a small web server in another language
> and this is more like copying code.
> i already have everything figured out, except this one but noone seems
> either willing or capable of helping me.
> again sorry but i was in a very bad mood.

Writing a webserver (you NEVER stated that that is your ultimate goal)
is a two-liner in Python.

See the module SimpleHTTPServer.

Using the extremely lowlevel module socket is a totally different beast.
It requires rather deep knowledge of unix sockets, and has a steep
learning curve.

Diez

Steve Holden

3/2/2008 5:38:00 PM

0

Gif wrote:
> i would like to apologize once more. i understand that you are saying
> "what a fool he is waiting for us to solve all his problems", cause
> i've said that for other posts, when they seemed "immature". It's just
> that i couldn't find a way out of 20 lines of code and this drove me
> mad.
>
> i end this topic here.

If you want to use the socket module, take a look at

http://holdenweb.com/py/n...

which contains links to some fairly explicit notes about how to write
TCP and UDP servers in Python.

Nobody thinks you are a fool for wanting help with your problems, it's
simply that you have to provide enough information about what' wring for
us to get a handle on the issues.

regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC http://www.hold...

Aaron Brady

3/2/2008 7:21:00 PM

0

On Mar 2, 11:38 am, Steve Holden <st...@holdenweb.com> wrote:
> Nobody thinks you are a fool for wanting help with your problems, it's
> simply that you have to provide enough information about what' wring for
> us to get a handle on the issues.

This worked:

import socket
from time import time

for i in range( 20 ):
HOST = ''
PORT = 80 #<----
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((HOST, PORT))
print( 'listen' )
s.listen(1)
conn, addr = s.accept()
print( 'connected', addr )
print( conn.recv( 4096 ) ) #<----
conn.send( bytes('<html><body>test %f</body></
html>'%time(),'ascii') )
conn.close() #<----
s.close()

... and connect with a browser: http://localhost/ if it's internet
exploder.