caw
caw

Reputation: 31497

Python: Read a file (from an external server)

Can you tell me how to code a Python script which reads a file from an external server? I look for something similar to PHP's file_get_contents() or file() function.

It would be great if someone could post the entire code for such a script.

Thanks in advance!

Upvotes: 0

Views: 3139

Answers (2)

Aaron
Aaron

Reputation: 829

better would be the same as Jarret's code, but using urllib2:

import urllib2
content = urllib2.urlopen('http://google.com').read()

urllib2 is a bit newer and more modern. Doesn't matter too much in your case, but it's good practice to use it.

Upvotes: 5

Jarret Hardie
Jarret Hardie

Reputation: 97962

The entire script is:

import urllib
content = urllib.urlopen('http://www.google.com/').read()

Upvotes: 12

Related Questions