First install the Python module:
pip install casjobs
Then set the environment variables:
export CAS_DR9_WSID="1234567890" # get yours here: http://skyserver.sdss3.org/CasJobs/ChangeDetails.aspx
export CAS_DR9_PSWD="your_casjobs_password"
Then run the demo... attached.
This saves a file called "stars.fits" with the table you want in the 1
HDU... I think that you know where to go from there.