473,387 Members | 1,925 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

tcp

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.
Mar 2 '08 #1
11 1310
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(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()
...
Mar 2 '08 #2
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(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/

Mar 2 '08 #3
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?
Mar 2 '08 #4
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-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
Mar 2 '08 #5
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.
Mar 2 '08 #6
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.
Mar 2 '08 #7
On 2008-03-02, Gif <pa***********@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!!
Mar 2 '08 #8
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
Mar 2 '08 #9
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/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
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC http://www.holdenweb.com/

Mar 2 '08 #10
On Mar 2, 11:38*am, Steve Holden <st...@holdenweb.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(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.
Mar 2 '08 #11
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.
Mar 2 '08 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: William C. White | last post by:
Does anyone know of a way to use PHP /w Authorize.net AIM without using cURL? Our website is hosted on a shared drive and the webhost company doesn't installed additional software (such as cURL)...
2
by: Albert Ahtenberg | last post by:
Hello, I don't know if it is only me but I was sure that header("Location:url") redirects the browser instantly to URL, or at least stops the execution of the code. But appearantely it continues...
3
by: James | last post by:
Hi, I have a form with 2 fields. 'A' 'B' The user completes one of the fields and the form is submitted. On the results page I want to run a query, but this will change subject to which...
0
by: Ollivier Robert | last post by:
Hello, I'm trying to link PHP with Oracle 9.2.0/OCI8 with gcc 3.2.3 on a Solaris9 system. The link succeeds but everytime I try to run php, I get a SEGV from inside the libcnltsh.so library. ...
1
by: Richard Galli | last post by:
I want viewers to compare state laws on a single subject. Imagine a three-column table with a drop-down box on the top. A viewer selects a state from the list, and that state's text fills the...
4
by: Albert Ahtenberg | last post by:
Hello, I have two questions. 1. When the user presses the back button and returns to a form he filled the form is reseted. How do I leave there the values he inserted? 2. When the...
1
by: inderjit S Gabrie | last post by:
Hi all Here is the scenerio ...is it possibly to do this... i am getting valid course dates output on to a web which i have designed ....all is okay so far , look at the following web url ...
2
by: Jack | last post by:
Hi All, What is the PHP equivilent of Oracle bind variables in a SQL statement, e.g. select x from y where z=:parameter Which in asp/jsp would be followed by some statements to bind a value...
3
by: Sandwick | last post by:
I am trying to change the size of a drawing so they are all 3x3. the script below is what i was trying to use to cut it in half ... I get errors. I can display the normal picture but not the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.