tcp

Steve Holden steve at holdenweb.com
Sun Mar 2 15:32:18 CET 2008


7stud wrote:
> On Mar 2, 6:09 am, Gif <pavloutefk... at 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.holdenweb.com/




More information about the Python-list mailing list