tcp

G

Gif

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.
 
7

7stud

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()
...
 
S

Steve Holden

7stud said:
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
 
G

Gif

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?
 
D

Diez B. Roggisch

Gif said:
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-questions.html

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
 
G

Gif

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.
 
G

Gif

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.
 
G

Grant Edwards

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.
 
D

Diez B. Roggisch

Gif said:
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
 
S

Steve Holden

Gif said:
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/networking/

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
 
C

castironpi

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.
 
G

Gif

thanks everybody, i've got this to work. i'm not trying to write an
actual web server, i'm just using it for some procedures like URL
rewriting.
 
G

Gabriel Genellina

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.

Note that there is no need (nor is desirable) to close and rebind the
listening socket for each connection. The loop should start with the
accept call, and end at the conn.close() call (s.close() is left out
of the loop).
And then you get a pretty standard server that handles one connection
at a time.
 
R

Roy Smith

Gabriel Genellina said:
Note that there is no need (nor is desirable) to close and rebind the
listening socket for each connection.

I'd say, "nor is desirable", is an understatement. On most systems, an
attempt to re-bind to a given port number soon after it was unbound will
fail (unless you utter magic ioctl incantations). This will manifest
itself in the s.bind() call raising an exception on the *second* pass
through the loop.
 
D

Dan Stromberg

I'd say, "nor is desirable", is an understatement. On most systems, an
attempt to re-bind to a given port number soon after it was unbound will
fail (unless you utter magic ioctl incantations). This will manifest
itself in the s.bind() call raising an exception on the *second* pass
through the loop.

I believe the incantation may be setsockopt(), not ioctl(), but if
there's an ioctl() way of doing it, i'd be interested in seeing it.
 

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,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top