0

I created a python server on port 8000 using python -m SimpleHTTPServer.

When I visit this url from my web browser it shows the below content

enter image description here

Now, I want to get the above content using python. So, for that what I did is

>>> import socket
>>> s = socket.socket(
...     socket.AF_INET, socket.SOCK_STREAM)
>>> s.connect(("localhost", 8000))
>>> s.recv(1024)

But after s.recv(1024) nothing happens it just wait there and prints nothing.

So, my question is how to get above directory content output using python. Also can someone suggest me a tutorial on socket programming with python. I didn't liked the official tutorial that much.

I also observed a strange thing when I try to receive contents using python and nothing happens at that time I cannot access localhost:8000 from my web browser but as soon as I kill my python program I can access it.

1
  • 1
    It's not socket programming what you need, but the HTTP protocol specification. Also, depending on what you are writing your program for, there are modules you can use that speak HTTP for you... Commented Jul 4, 2012 at 15:42

2 Answers 2

4

Arguably the simplest way to get content over http in python is to use the urllib2 module. For example:

from urllib2 import urlopen
f = urlopen('http://localhost:8000')
for line in f:
    print line

This will print out the file hosted by SimpleHTTPServer.

Sign up to request clarification or add additional context in comments.

2 Comments

So, if I understand what I am trying to achieve is not possible using sockets right?
@Noob Well, for what I think you're trying to do, you need sockets, but you don't need to directly use them. Essentially IP uses sockets, TCP uses IP, and HTTP uses TCP. You don't want to rebuild all of that just to access the data on a website.
2

But after s.recv(1024) nothing happens it just wait there and prints nothing.

You simply open a socket and waiting for the data, but it's not how HTTP protocol works. You have to send a request first if you want to receive a response (basically, you have to tell the server which directory you want to list or which file to download). If you really want to, you can send the request using raw sockets to train your skills, but the proper library is highly recommended (see Matthew Adams' response and urllib2 example).

I also observed a strange thing when I try to receive contents using python and nothing happens at that time I cannot access localhost:8000 from my web browser but as soon as I kill my python program I can access it.

This is because SimpleHTTServer is single-threaded and doesn't support multiple connections simultaneously. If you would like to fix it, take a look at the answers here: BasicHTTPServer, SimpleHTTPServer and concurrency.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.