-2

I am working on an open-source project called RubberBand which is an open source project that allows you to do what the title says. Locally execute python file that is located on a web server, however I have run a problem. If a comma is located in a string (etc. "http:"), It Will return an error.

'''
RubberBand Version 1.0.1 'Indigo-Charlie'
http://www.lukeshiels.com/rubberband

CHANGE-LOG:

Changed Error Messages.
Changed Whole Code Into one function, rather than three.
Changed Importing required libraries into one line instead of two

'''
#Edit Below this line

import httplib, urlparse

def executeFromURL(url):
    if (url == None):
        print "!# RUBBERBAND_ERROR: No URL Specified #!"
    else:
        CORE = None
        good_codes = [httplib.OK, httplib.FOUND, httplib.MOVED_PERMANENTLY]

    host, path = urlparse.urlparse(url)[1:3]
    try:
        conn = httplib.HTTPConnection(host)
        conn.request('HEAD', path)
        CORE = conn.getresponse().status

    except StandardError:
        CORE = None

    if(CORE in good_codes):
        exec(url)
    else:
        print "!# RUBBERBAND_ERROR: File Does Not Exist On WEBSERVER #!"
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • 4
    You can do that in one line of shell code: `wget http://some-url.com/path | python`. – Thomas Jun 05 '12 at 12:47
  • 1
    @Thomas - almost: `wget -O - http://some-url.com/path | python` – mata Jun 05 '12 at 13:11
  • There are higher level APIs for getting a file via HTTP than `httplib`, for example `requests` or `urllib2`. See an example in my answer below. – schlamar Jun 05 '12 at 13:44

4 Answers4

3

RubberBand in three lines without error checking:

import requests
def execute_from_url(url): 
    exec(requests.get(url).content)
schlamar
  • 9,238
  • 3
  • 38
  • 76
0

You should use a return statement in your if (url == None): block as there is no point in carrying on with your function.

Where abouts in your code is the error, is there a full traceback as URIs with commas parse fine with the urlparse module.

Is it perhaps httplib.ResponseNotReady when calling CORE = conn.getresponse().status?

Nevermind that error message, that was me quickly testing your code and re-using the same connection object. I can't see what would be erroneous in your code.

Christian Witts
  • 11,375
  • 1
  • 33
  • 46
  • Should I try downloading the file, and then, running it, and then deleting it immediately after ran successfully? – gameglicher Jun 05 '12 at 13:03
  • Actually just realised what your error was, you're attempting to execute the URL and not the Python script that you retrieved. After your request, you should get the reponse, and then check the status code of the response and if it's good to go, then you can exec what comes out of `.read()` – Christian Witts Jun 05 '12 at 13:33
0

I would suggest to check this question.

avoid comma in URL, that my suggestion.

Can I use commas in a URL?

Community
  • 1
  • 1
dilip kumbham
  • 703
  • 6
  • 15
  • I have figured it out and reduced the file size (uncompressed, not including documentation) to 549 Bytes. I have updated to version 2.0 on the website to Romeo! Please check it out and tell the internet about it! It might be a simple project, but it really means a lot to me, and can be used to run your python project, but will always be up-to-date, no matter what. (That reminds me, what about if you have no internet connection? version 2.0.1 Romeo-Juliet coming very soon!) – gameglicher Jun 05 '12 at 13:53
  • I used mata's version, and it works great. You can download it at http://lukeshiels.com/rubberband and edit it if you want. – gameglicher Jun 05 '12 at 14:32
0

This seems to work well for me:

import urllib
(fn,hd) = urllib.urlretrieve('http://host.com/file.py')
execfile(fn)

I prefer to use standard libraries, because I'm using python bundled with third party software (abaqus) which makes it a real headache to add packages.