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.

RanRag
  • 48,359
  • 38
  • 114
  • 167
  • 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... – deStrangis Jul 04 '12 at 15:42

2 Answers2

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.

Matthew Adams
  • 9,426
  • 3
  • 27
  • 43
  • So, if I understand what I am trying to achieve is not possible using sockets right? – RanRag Jul 04 '12 at 15:52
  • 1
    @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. – Matthew Adams Jul 04 '12 at 16:02
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.

Community
  • 1
  • 1
tomasz
  • 12,574
  • 4
  • 43
  • 54