0

I have a website made in PHP.

To increase number of data sets in my database, I need to create a python script such that I need not add 500 registrations manually.

There are several tools available but I need to create script of my own.

Can any one help me with this ?

PS: I have knowledge of PHP, Python and ASP.NET as well.

vipul_surana
  • 114
  • 2
  • 5
  • 21
  • It would be easier to write your testing code in PHP. Get a testing framework (or make your own), and use it to invoke the code that registers users. – Asad Saeeduddin Mar 21 '14 at 06:45
  • @Asad, The reason I desire to write a Python script is my next project is gonna be on python, so better late than never. – vipul_surana Mar 21 '14 at 06:48
  • "I have knowledge of Python", yet you ask a basic question regarding Python without telling us what database engine you're using? MySQL? – Torxed Mar 21 '14 at 06:50

2 Answers2

1

MySQL

import MySQLdb

db = MySQLdb.connect(host="localhost", user="john", passwd="megajonhy", db="jonhydb")
cursor = db.cursor() 

for i in range(0,500):
    cursor.execute("INSERT INTO MyTable VALUES('Some string', 1337);")

PostgreSQL

import postgresql
db = postgresql.open("pq://postgres:SupaPass@127.0.0.1/testdb")
prepstatement = db.prepare("INSERT INTO MyTable VALUES($1, $2, $3);")
with db.xact():
    for i in range(0, 500):
        prepstatement('Some string', 1337, ["a", "list"])

MsSQL

import pyodbc
cnxn = pyodbc.connect('DRIVER={SQL Server};SERVER=localhost;DATABASE=testdb;UID=user;PWD=pass')
cursor = cnxn.cursor()
for i in range(0, 500):
    cursor.execute("INSERT INTO MyTable VALUES('Some string', 1337);")

SQLAlchemy

Note that this is a library that will do a lot of magic for you, hence you'd might not learn as much from it or desire all it's functionality.

from sqlalchemy import create_engine
db = create_engine("mssql://me:pass@localhost/testdb")
for i in range(0, 500):
    db.execute("INSERT INTO MyTable VALUES('Some string', 1337);"):

How get get POST/GET data

And finally, we have no clue as to how you run the script.
But you mentioned web development and well, assuming you run the script as CGI, here's how to get the POST/GET data:

import cgi
form = cgi.FieldStorage()
print form["username"]

Let me Google this for you

Community
  • 1
  • 1
Torxed
  • 22,866
  • 14
  • 82
  • 131
0

You'll find a lot of Web testing tools in this page: https://wiki.python.org/moin/PythonTestingToolsTaxonomy#Web_Testing_Tools

However I would like to recommend another python module.

I understood that the script python will access to your web page and It'll simulate a human registration, if this assumption is right, you can use "requests" module, It's not a testing tool exactly, but It's a very easy way to access and recover web content, for instance:

You can manage http session (source: http://docs.python-requests.org/en/latest/user/advanced/#session-objects):

s = requests.Session()
s.auth = ('user', 'pass')
s.headers.update({'x-test': 'true'})

# both 'x-test' and 'x-test2' are sent
s.get('http://httpbin.org/headers', headers={'x-test2': 'true'})

An whatever authentication method (source: http://docs.python-requests.org/en/latest/user/authentication/):

>>> from requests.auth import HTTPDigestAuth
>>> url = 'http://httpbin.org/digest-auth/auth/user/pass'
>>> requests.get(url, auth=HTTPDigestAuth('user', 'pass'))
<Response [200]>
Roberto
  • 8,586
  • 3
  • 42
  • 53