Python script to connect to hosted database on Namecheap - python

EDIT 1
It looks like I can import the sqlite3 package. Now I'm working on figuring out how to specify the IP address of the hosted server so I can connect to it. It's not looking likely though.
EDIT 2
It looks like sqlite3 is not designed to access hosted databases. The search continues.
This is a pretty basic question, however I haven't found any resources on Namecheap's website, here, or through Google searches that shed any light on my problem. I'm trying to write a python script that will query my hosted database to retrieve records. So far I haven't gotten past the first step of importing a module and this is my code:
#!/usr/bin/python
print "Content-type: text/html\n\n"
import cgitb
cgitb.enable()
import PyMySQL
cgitb works fine and prints out the following error message:
I also tried importing MySQLdb and received the same error. Does anyone have experience with databases, python, and namecheap?

Related

Using Wikipedia and Requests in CGI without installing

I have a simple mashup CGI program written in Python (2.6) that, among other sources, should grab the summary of Wikipedia articles to present to the user along with results from several other sources (as it is what mashups are supposed to do). Unfortunately I have no access to the console on the remote server and therefore I cannot install any modules using pip, easy_install, etc. Following the suggestion from here, I have been able to use BeautifulSoup4. However, I had no success when attempting to use the same strategy (unpacking the source directory and uploading it the cgi-bin directory in the remote server) with the wikipedia and requests modules.
Here is the test script I am using:
#!/usr/bin/python
import cgitb
cgitb.enable()
import urllib
import urllib2
from bs4 import BeautifulSoup
#import requests
#import wikipedia
print "Content-type: text/html\n\n"
print "<html><head><title>CGI Demo</title></head>"
print "<h1>Hello World</h1>"
print "</html>"
If I enable the lines that import the requests and wikipedia modules, I get the dreadful "500 Internal Server Error", otherwise the script runs OK.
So, my question is, is it possible to use the wikipedia and requests modules (in fact, wikipedia depends upon requests) without installing them in the usual ways?
Thanks in advance for any hints or suggestions.

Adwords, Python Import Script. MySQL goes fubar

I wrote a Python 2.7 script based on the Python examples from google Adwords on github, installed the MySQL Connector from MySQL Page itself under Connectors.
And started with creating the script, but I am not able to find the bug which is causing that MySQL stops insert data or not even goes into the function.
No error messages, even removed all try and catch. Same.
If you like to check it for yourself: http://pastebin.com/WEQqBNHB
I used 5 Connections, because the functions call each other and it seems like if while is used you need one connection for it seperated.
MySQL Syntax is okay, as phpmyadmin is telling me.
Everything works exept Adgroups_AD/Adgroups_Performance
Any Ideas?
It seems like that i even need to commit select query's in python.
That fixed the issue. dang.

Importing Python files into each other on a web server

Using CGI scripts, I can run single Python files on my server and then use their output on my website.
However, I have a more complicated program on my computer that I would like to run on the server. It involves several modules I have written myself, and the SQLITE3 module built in Python. The program involves reading from a .db file and then using that data.
Once I run my main Python executable from a browser, I get a "500: Internal server error" error.
I just wanted to know whether I need to change something in the permission settings or something for Python files to be allowed to import other Python files, or to read from a .db file.
I appreciate any guidance, and sorry if I'm unclear about anything I'm new to this site and coding in general.
FOLLOW UP: So, as I understand, there isn't anything inherently wrong with importing Python files on a server?
I suggest you look in the log of your server to find out what caused the 500 error.
You can get an extensive error message by adding the following lines at the beginning of your CGI script:
import cgitb
cgitb.enable()

Django on Strato webspace

I have a web space package at Strato and they say that I can use Python on their webspace. Currently I use PHP with CakePHP since PHP works, but I'd rather use Python with some framework, where Django seems to be the match.
So I uploaded a little script into /cgi-bin/test.py:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
print "Content-Type: text/html"
print
print "Hello, World!"
And it indeed prints Hello, World!.
Then I tried to import django below the “Hello, World!“:
try:
import django
except Exception as e:
print e
All I got is an “Internal Server Error“.
Does it make sense to use this server for Python at all, or is that just some half hearted support that will not be fun anyway?
The import doesn't work because you haven't set up the required environment variable. However, even if you managed to get the import working, Django deployment is a bit more involved than putting a script in your cgi-bin directory. You need to choose a method such as WSGI, or FastCGI, and configure your webserver (Apache/Nginx/etc).
You have three categories of hosts for deploying Django.
a shared web host that is Django friendly. I can thoroughly recommend djangohosting.ch, which I used until I started working for a web hosting company myself.
a VPS (this can be more tricky, because you have to manage more yourself, but you will have more flexibility and better performance that shared hosting)
a platform as a service (PaaS) such as ep.io or heroku.

Uploading a mysql database to a webserver supporting python

I have a written a very small web-based survey using cgi with python(This is my first web app. ).The questions are extracted from a MySQL database table and the results are supposed to be saved in the same database. I have created the database along with its table locally. My app works fine on my local computer(localhost). To create db,table and other transaction with the MySQL i had to do import MySQLdb in my code.
Now I want to upload everything on my personal hosting. As far as I know my hosting supports Python,CGI and has MySQL database. And I know that I have to change some parameters in the connection string in my code, so I can connect to the database, but I have two problems:
I remember that I installed MySQLdb as an extra to my Python, and in my code i am using it, how would I know that my hosting's python interpretor has this installed, or do I even need it, do I have to use another library?
How do I upload my database onto my hosting?
Thanks
If you have shell access, you can fire up the python interpreter by running python and type import MySQLdb at the >>> prompt. If you get no errors in return, then its installed.
Likewise, if you have shell access, this page will help you with importing and exporting using the mysql command. I found it by googleing "import export mysql".
You can write a simple script like
import MySQLdb and catch any errors
to see if the required package is
installed. If this fails you can ask
the hosting provider to install your
package, typically via a ticket
The hosting providers typically also provide URL's to connect to the MySQL tables they provision for you, and some tools like phpmyadmin to load database dumps into the hosted MySQL instance
To check if MySQLdb library is installed on your hosting, simply open a python shell and type: import MySQLdb. If everthing goes ok, you're readt to go. If you get: ImportError: No module named MySQLdb, that means the the library is not installed and you nedd to install it.
You need that library or some library that provides similar support, because Python does not support native access to MySQL databases.
To transfer you database to your hosting check mysqldump.

Categories