tcp

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Gif

    tcp

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

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

    #! /usr/bin/python
    import socket

    HOST = ''
    PORT = 1515
    s = socket.socket(s ocket.AF_INET, socket.SOCK_STR EAM)
    s.bind((HOST, PORT))
    s.listen(1)
    conn, addr = s.accept()
    conn.send('HTTP/1.1 200 OK\r\n')
    conn.send('Cont ent-Type: text/html\r\n')
    conn.send('Serv er: test/1.0\r\n\r\n')
    conn.send('<htm l><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,whi le and all kinds of loops but
    always new erros pop up, or it overflows.
  • 7stud

    #2
    Re: tcp

    On Mar 2, 6:09 am, Gif <pavloutefk...@ gmail.comwrote:
    i have this small script which after some router configurations works.
    >
    ############### ############### ############### #############
    >
    #! /usr/bin/python
    import socket
    >
    HOST = ''
    PORT = 1515
    s = socket.socket(s ocket.AF_INET, socket.SOCK_STR EAM)
    s.bind((HOST, PORT))
    s.listen(1)
    conn, addr = s.accept()
    conn.send('HTTP/1.1 200 OK\r\n')
    conn.send('Cont ent-Type: text/html\r\n')
    conn.send('Serv er: test/1.0\r\n\r\n')
    conn.send('<htm l><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,whi le and all kinds of loops but
    always new erros pop up, or it overflows.
    while True:
    conn, addr = s.accept()
    ...

    Comment

    • Steve Holden

      #3
      Re: tcp

      7stud wrote:
      On Mar 2, 6:09 am, Gif <pavloutefk...@ gmail.comwrote:
      >i have this small script which after some router configurations works.
      >>
      >############## ############### ############### ##############
      >>
      >#! /usr/bin/python
      >import socket
      >>
      >HOST = ''
      >PORT = 1515
      >s = socket.socket(s ocket.AF_INET, socket.SOCK_STR EAM)
      >s.bind((HOST , PORT))
      >s.listen(1)
      >conn, addr = s.accept()
      >conn.send('HTT P/1.1 200 OK\r\n')
      >conn.send('Con tent-Type: text/html\r\n')
      >conn.send('Ser ver: test/1.0\r\n\r\n')
      >conn.send('<ht ml><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,whi le 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/

      Comment

      • Gif

        #4
        Re: tcp

        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?

        Comment

        • Diez B. Roggisch

          #5
          Re: tcp

          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?


          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

          Comment

          • Gif

            #6
            Re: tcp

            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.

            Comment

            • Gif

              #7
              Re: tcp

              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.

              Comment

              • Grant Edwards

                #8
                Re: tcp

                On 2008-03-02, Gif <pavloutefkros@ gmail.comwrote:
                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!!

                Comment

                • Diez B. Roggisch

                  #9
                  Re: tcp

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

                  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

                  Comment

                  • Steve Holden

                    #10
                    Re: tcp

                    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



                    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.holdenweb.com/

                    Comment

                    • castironpi@gmail.com

                      #11
                      Re: tcp

                      On Mar 2, 11:38 am, Steve Holden <st...@holdenwe b.comwrote:
                      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(s ocket.AF_INET, socket.SOCK_STR EAM)
                      s.bind((HOST, PORT))
                      print( 'listen' )
                      s.listen(1)
                      conn, addr = s.accept()
                      print( 'connected', addr )
                      print( conn.recv( 4096 ) ) #<----
                      conn.send( bytes('<html><b ody>test %f</body></
                      html>'%time(),' ascii') )
                      conn.close() #<----
                      s.close()

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

                      Comment

                      • Gif

                        #12
                        Re: tcp

                        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.

                        Comment

                        Working...