2

I made a script in my Django called "bdd.py" inside "polls" as you can see in this tree : enter image description here

My problem is that this script is supposed to send some datas into my table torrent_infos in my MySQL database "TORRENTS" but i don't see anything in my table. Indeed when i execute the script it seems working:

root@debian:/home/florian/Documents/mysite/polls# python bdd.py

But my table still empty and i don't understand why. Have a look at my database:

mysql> DESCRIBE torrent_infos;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| Name  | varchar(50) | YES  |     | NULL    |       |
| Size  | varchar(50) | YES  |     | NULL    |       |
| Hash  | varchar(60) | YES  |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)

mysql> SELECT * FROM torrent_infos ;
Empty set (0.01 sec)

And this is my script, probably the roots of the problem as you can see i take informations from the .torrent files in a folder and i'm trying to send it into my table torrent_infos. I tried some changes in the indentation without success. Any idea in mind are welcomed :

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import mysql.connector
import bencode
import binascii
import hashlib
import os
import sys

conn = mysql.connector.connect(host="localhost",user="root",password="root", database="TORRENTS")
cursor = conn.cursor()
path = "/home/florian/TorrentFiles"
dirs = os.listdir(path)
for file in dirs:
        try:
                with open(os.path.join(path, file), 'rb') as torrentfile:
                        torrent = bencode.bdecode(torrentfile.read())
                        user = ("torrent['info']['name']","torrent['info']['length']","(hashlib.sha1(bencode.bencode(torrent['info'])).hexdigest())")
                        cursor.execute("""INSERT INTO torrent_infos (Name, Size, Hash) VALUES(%s, %s, %s)""", user)
        except bencode.BTL.BTFailure:
                continue


conn.close()
7
  • 3
    Since this is a django app, I was wondering why don't you use the django framework for your script. docs.djangoproject.com/en/1.8/howto/custom-management-commands Commented May 27, 2015 at 10:51
  • 3
    You did not call conn.commit(), so your changes get rolled back when you disconnect. Still, I think you should follow Wtower's advice and implement this kind of things as a Django managemenet command. Commented May 27, 2015 at 11:01
  • @Wtower: Thank you, actually i was wondering were can i find information about it i will follow this doc :) Commented May 27, 2015 at 11:10
  • @Wtower But i don't really understand the advantage of using this Commented May 27, 2015 at 11:20
  • Why have you used django for this app at the first place? Commented May 27, 2015 at 11:22

1 Answer 1

2

I would rewrite the above to fully use the django framework as a custom management command:

  • Create a directory in polls/ named management
  • Create an empty __init__.py file in polls/management/
  • Create a directory in polls/management/ named commands
  • Create an empty __init__.py file in polls/management/commands/
  • Create a bdd.py file in polls/management/commands/
  • In bdd.py define a command class that inherits from django.core.management.base.BaseCommand as in the example of documentation
  • Create (if you haven't done so) the appropriate models for torrent_infos in your models.py and import in bdd.py
  • Define the command logic properly using Django ORM.

In this way, you can fully take advantage of the Django framework, your app's models etc.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.