473,545 Members | 2,663 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

question about python

I was working through a tutorial about how to write a server using
python (the url is bellow). I am sure that the server is working to
some degree because when the server is running localhost:8080 just
keeps trying to load until it times out. I would like to know how to
send information through the server to my browser?

I was working through this tutorial:
http://python.about.com/od/networkin...nWebServer.htm
and my final code is like this:

import socket

host = ''
port = 8080

c = socket.socket(s ocket.AF_INET, socket.SOCK_STR EAM)

c.setsockopt(so cket.SOL_SOCKET , socket.SO_REUSE ADDR, 1)

c.bind((host, port))

c.listen(1)

while 1:
csock, caddr = c.accept()
cfile = csock.makefile( 'rw', 0)

line = cfile.readline( ).strip()

cfile.write('HT TP/1.0 200 OK\n\n')
cfile.write('<h tml><head><titl e>Welcome %s!</title></head>' %
(str(caddr)))
cfile.write('<b ody><h1>Follow the link...</h1>')
cfile.write('Al l the server needs to do is ')
cfile.write('to deliver the text to the socket. ')
cfile.write('It delivers the HTML code for a link, ')
cfile.write('an d the web browser converts it. <br><br><br><br >')
cfile.write('<f ont size="7"><cente r<a href="http://python.about.co m/
index.html">Cli ck me!</a</center></font>')
cfile.write('<b r><br>The wording of your request was: "%s"' %(line))
cfile.write('</body></html>')

cfile.close()
csock.close()
Sep 13 '08 #1
5 1343
fishfin schrieb:
I was working through a tutorial about how to write a server using
python (the url is bellow). I am sure that the server is working to
some degree because when the server is running localhost:8080 just
keeps trying to load until it times out. I would like to know how to
send information through the server to my browser?

I was working through this tutorial:
http://python.about.com/od/networkin...nWebServer.htm
and my final code is like this:

import socket

host = ''
port = 8080

c = socket.socket(s ocket.AF_INET, socket.SOCK_STR EAM)

c.setsockopt(so cket.SOL_SOCKET , socket.SO_REUSE ADDR, 1)

c.bind((host, port))

c.listen(1)

while 1:
csock, caddr = c.accept()
cfile = csock.makefile( 'rw', 0)

line = cfile.readline( ).strip()

cfile.write('HT TP/1.0 200 OK\n\n')
cfile.write('<h tml><head><titl e>Welcome %s!</title></head>' %
(str(caddr)))
cfile.write('<b ody><h1>Follow the link...</h1>')
cfile.write('Al l the server needs to do is ')
cfile.write('to deliver the text to the socket. ')
cfile.write('It delivers the HTML code for a link, ')
cfile.write('an d the web browser converts it. <br><br><br><br >')
cfile.write('<f ont size="7"><cente r<a href="http://python.about.co m/
index.html">Cli ck me!</a</center></font>')
cfile.write('<b r><br>The wording of your request was: "%s"' %(line))
cfile.write('</body></html>')

cfile.close()
csock.close()
Do yourself a favor and look into the various python webframeworks such
as TurboGears, Django and what not.

Or at least into the module SimpleHTTPServe r.

Diez
Sep 13 '08 #2
On Sep 13, 12:15 am, fishfin <calebjhan...@g mail.comwrote:
I was working through a tutorial about how to write a server using
python (the url is bellow). I am sure that the server is working to
some degree because when the server is running localhost:8080 just
keeps trying to load until it times out. I would like to know how to
send information through the server to my browser?

I was working through this tutorial:http://python.about.com/od/networkin...nWebServer.htm

and my final code is like this:

import socket

host = ''
port = 8080

c = socket.socket(s ocket.AF_INET, socket.SOCK_STR EAM)

c.setsockopt(so cket.SOL_SOCKET , socket.SO_REUSE ADDR, 1)

c.bind((host, port))

c.listen(1)

while 1:
csock, caddr = c.accept()
cfile = csock.makefile( 'rw', 0)
It looks like everything after this line needs to be indented.
Besides that, nothing jumps out at me, though I don't do direct socket
programming a lot.
line = cfile.readline( ).strip()

cfile.write('HT TP/1.0 200 OK\n\n')
cfile.write('<h tml><head><titl e>Welcome %s!</title></head>' %
(str(caddr)))
cfile.write('<b ody><h1>Follow the link...</h1>')
cfile.write('Al l the server needs to do is ')
cfile.write('to deliver the text to the socket. ')
cfile.write('It delivers the HTML code for a link, ')
cfile.write('an d the web browser converts it. <br><br><br><br >')
cfile.write('<f ont size="7"><cente r<a href="http://python.about.co m/
index.html">Cli ck me!</a</center></font>')
cfile.write('<b r><br>The wording of your request was: "%s"' %(line))
cfile.write('</body></html>')

cfile.close()
csock.close()

Carl Banks
Sep 13 '08 #3
@ Carl: Yes, I think your right now that I look at it (or at least all
except for the last two lines need to be indented). I'm still not sure
how to send the stuff to the web browser though. Thanks for pointing
it out!

@ Diez: I'll start googling those right away.

Carl Banks wrote:
On Sep 13, 12:15 am, fishfin <calebjhan...@g mail.comwrote:
I was working through a tutorial about how to write a server using
python (the url is bellow). I am sure that the server is working to
some degree because when the server is running localhost:8080 just
keeps trying to load until it times out. I would like to know how to
send information through the server to my browser?

I was working through this tutorial:http://python.about.com/od/networkin...nWebServer.htm

and my final code is like this:

import socket

host = ''
port = 8080

c = socket.socket(s ocket.AF_INET, socket.SOCK_STR EAM)

c.setsockopt(so cket.SOL_SOCKET , socket.SO_REUSE ADDR, 1)

c.bind((host, port))

c.listen(1)

while 1:
csock, caddr = c.accept()
cfile = csock.makefile( 'rw', 0)

It looks like everything after this line needs to be indented.
Besides that, nothing jumps out at me, though I don't do direct socket
programming a lot.
line = cfile.readline( ).strip()

cfile.write('HT TP/1.0 200 OK\n\n')
cfile.write('<h tml><head><titl e>Welcome %s!</title></head>' %
(str(caddr)))
cfile.write('<b ody><h1>Follow the link...</h1>')
cfile.write('Al l the server needs to do is ')
cfile.write('to deliver the text to the socket. ')
cfile.write('It delivers the HTML code for a link, ')
cfile.write('an d the web browser converts it. <br><br><br><br >')
cfile.write('<f ont size="7"><cente r<a href="http://python.about.co m/
index.html">Cli ck me!</a</center></font>')
cfile.write('<b r><br>The wording of your request was: "%s"' %(line))
cfile.write('</body></html>')

cfile.close()
csock.close()


Carl Banks
Sep 13 '08 #4
On Sep 13, 1:00 am, fishfin <calebjhan...@g mail.comwrote:
@ Carl: Yes, I think your right now that I look at it (or at least all
except for the last two lines need to be indented). I'm still not sure
how to send the stuff to the web browser though. Thanks for pointing
it out!
Try reading in the whole HTTP request instead of just the first line.
Change

line = cfile.readline( ).strip()

to

line = cfile.read()

And see if that helps. (Outputting the document so that it formats
the request well is left as an exercise. Also, as a heads up: in real
programs you should never output anything you receive through the
network without checking it or escaping it to prevent malicious uses.)
Carl Banks
Sep 13 '08 #5
On Sep 13, 4:25*pm, Carl Banks <pavlovevide... @gmail.comwrote :
On Sep 13, 1:00 am, fishfin <calebjhan...@g mail.comwrote:
@ Carl: Yes, I think your right now that I look at it (or at least all
except for the last two lines need to be indented). I'm still not sure
how to send the stuff to the web browser though. Thanks for pointing
it out!

Try reading in the whole HTTP request instead of just the first line.
Change

line = cfile.readline( ).strip()

to

line = cfile.read()

And see if that helps. *(Outputting the document so that it formats
the request well is left as an exercise. *Also, as a heads up: in real
programs you should never output anything you receive through the
network without checking it or escaping it to prevent malicious uses.)

Carl Banks
I figured out what the problem was. When you had suggested that I
indent the lines at first I did all of them, but when I did that there
must have been an nonindented line before the last two lines which I
had indented, so ending the 'while 1:'. Because of that, that code
just flat out didn't work so I assumed that they must be not be
indented which is why it hasn't been working all along. Thanks for
your help! I don't think I would have every figured it out if you last
post hadn't gotten me to thinking about little tweeks like that.
Sep 13 '08 #6

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

Similar topics

4
1526
by: Jeffrey Borkent | last post by:
Hi There, I have an open source program which i am trying to add to. class classname(member,member) def __init__(self,list) self.__list = list self.refresh = 360
4
2109
by: Martitza | last post by:
Hi. I work for a small company (actually in process of forming) interested in embedding or extending python as part of our commercial non-open-source product. We have legal counsel, but are interested in the spirit as well as the letter of the law. Not much seems to have been written about the python license since version 2, so pointers to...
5
1801
by: Steven W. Orr | last post by:
I'm trying again, since no response indicates that I'm not providing enough info. I have module M1 which has the following line in it: StartTime = safe_dict_get ( dic, 'starttime', 0xFFFFFFFF ) It gets imported by modules M2 and M3. And finally, M4 imports both M2 and M3. So the idea is that in total we have 4 files called M1.py M2.py...
0
1298
by: Stodge | last post by:
Hi folks, new to Boost Python and struggling to build a prototype at work. I thought I'd start with a conceptual question to help clarify my understanding. I already have a basic prototype working nicely but I'm having a few issues, which I may post about later. A brief functional rundown of what I'm trying to prototype. Hopefully my...
1
1156
by: SimonPalmer | last post by:
Apologies in advance if this is either a) the wrong board or b) been answered a million times elsewhere, but... I have been given an assignment to get a python module up and running behind an existing web site. At the moment the rest of the site is developed in PHP but the hosts have said they will provide python support for free, although...
0
7490
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7682
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7935
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7449
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7780
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6009
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5351
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
1
1911
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1037
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.