Archive for the ‘Python’ Category

Python Python Parse Örneği (Düzenli İfadeler, MySQL)

0 Comments

# Bu program Ali Okan YÜKSEL tarafından kodlanmıştır. Öneri, görüş ve sorularınız  için aokany@gmail.com adresine yazabilirsiniz.
import re
import MySQLdb
 
 
 
db=MySQLdb.Connection(host="localhost",user="root",passwd="",db="deucbs")
cursor=db.cursor()
 
cursor = db.cursor()
 
 
 
p1 = re.compile('.*<font face="Times" size="2">|<font size="2" face="Times">.*')
p2 = re.compile('.*<b>(.+)<\/b>.*')
p3 = re.compile('<div.*left:(.+)px;">(.+)</div>.*')
p4 = re.compile('.*<hr>.*')
 
 
fileIN = open( "kamyoncular/izmirkamyoncular_Part1.htm", "r")
 
line = fileIN.readline()
text = ""
yaz = ""
sonraki = 0
linenum = 0
myline =""
 
while line:
    line = fileIN.readline()
    if p1.match(line):
        sonraki = '1'
        yaz = '1'
        print """
"""
        linenum = linenum+1
 
    if sonraki == '1':
        if p2.match(line):
            text += """
    </tr>
    <tr>
    """
            sonraki = '0'
            print """ > """+p2.match(line).group(1)+"""
 
"""
 
 
 
    if yaz == '1':
        if p3.match(line):
            if p4.match(line)==None:
                lineid = p3.match(line).group(1).replace(' ','')
                if(lineid!=""):
                    #print lineid + "==>" + p3.match(line).group(2)
                    #if lineid == "963":
 
                    deger = p3.match(line).group(2)
 
                    if deger[0:3]!="<b>":
#                        strSQL = "INSERT INTO `veri` (`dosya`, `satir`, `sirano`, `kolon`, `veri`, `ex`) VALUES('izmirkamyoncular_Part1.htm', '"+ str(linenum) +"', '0', '"  + lineid+ "', '" + deger + "', '0')"
#                        print strSQL+""""""
#                        cursor.execute (strSQL)
                         print lineid + "==>" + p3.match(line).group(2)
 
 
 
 
 
 
 
 
fileIN.close()
print myline
text = """<table>
<tr>
""" + text + """
</tr></table>"""
 
#log_file = open("converted_1.htm", "w")
#log_file.write(text)
#log_file.close()

Python Tkinter Socket Threading, IRC Bot

0 Comments

#!/usr/bin/python
# Bu program Ali Okan YÜKSEL tarafindan yazilmistir.
# Oneri gorus ve sorulariniz icin: hello@knyksl.com
# Kodlarin isinize yarayacagini umuyorum.
 
from Tkinter import *
import socket
import threading
import time
 
class ircx(threading.Thread):
    def tanimla(arg0,arg1,arg2,arg3):
        global server1,nick1,kanal1
        server1 = arg1
        nick1 = arg2
        kanal1 = arg3
    def run(self):
        self.server = server1
        self.nick = nick1
        self.channel = kanal1
        self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.socket.connect((self.server, 6667))
        self.socket.send('NICK %s\r\n' % self.nick)
        self.socket.send('USER X 0 * : fazbot\r\n')
        self.socket.send('JOIN %s\r\n' % self.channel)
        self.socket.send('PRIVMSG #snakepit :Hey \r\n')
        self.socket.send('PRIVMSG %s :Merhabalar, yardim icin !help yazin.\r\n' % self.channel)
 
        while 1:
             self.data = self.socket.recv(1024)
             print self.data
        if self.data.startswith("PING"):
             self.socket.send('PONG %s\r\n' % self.data.split(":")[1])
             time.sleep(3)
 
class irc:
    def __init__(arg0,arg1,arg2,arg3):
        #print arg1 + arg2
        baglan = ircx()
        baglan.tanimla(arg1,arg2,arg3)
        baglan.start()
 
class ana:
    def __init__(self,parent):
        global ent1,ent2,ent3,but1
        self.anaframe = Frame(parent)
        self.anaframe.pack(pady=40)
 
        self.frame1 = Frame(self.anaframe)
        self.frame2 = Frame(self.anaframe)
        self.frame3 = Frame(self.anaframe)
        self.frame4 = Frame(self.anaframe)
        self.frame5 = Frame(self.anaframe)
 
        self.frame1.grid(row=0,column=0)
        self.frame2.grid(row=1,column=0,pady=5)
        self.frame3.grid(row=2,column=0,pady=5)
        self.frame4.grid(row=3,column=0,pady=5)
        self.frame5.grid(row=4,column=0,pady=10)
 
        ent1 = Entry(self.frame1)
        ent1.insert(END,"irc.freenode.net")
        ent2 = Entry(self.frame2)
        ent2.insert(END,"Klavier_bot")
        ent3 = Entry(self.frame3)
        ent3.insert(END,"#snakepit")
        self.lab1 = Label(self.frame1,text = "Server")
        self.lab2 = Label(self.frame2,text = "Nick")
        self.lab3 = Label(self.frame3,text = "Kanal")
        self.lab4 = Label(self.frame5,text = "Bu program Ali Okan YÜKSEL \ntarafindan ornek amacli yazimistir.\nE-posta: hello@knyksl.com")
 
        self.lab1.pack(side = "left")
        ent1.pack()
        self.lab2.pack(side="left",padx=7)
        ent2.pack()
        self.lab3.pack(side="left",padx=4)
        ent3.pack()
        self.lab4.pack()
        but1 = Button(self.frame4, text = "Connect",command=self.irc0)
        but1.pack()
 
    def irc0(arg0):
        irc1 = irc(ent1.get(),ent2.get(),ent3.get())
        but1["text"]="Connecting..."
    def butset(arg0,arg1):
        but1["text"]=arg1
 
root = Tk()
root.geometry("300x300+100+100")
ana1 = ana(root)
root.mainloop()

Python Simple Socket application

0 Comments

3131 no`lu port dinleniyor. Telnet ile bağlanmaya çalıştığınız client a dönüş yapıyor.

import socket, traceback
# Sample Socket Application Ali Okan YÜKSEL okan@deu.edu.tr
host = '127.0.0.1'
port = 3131
 
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind((host, port))
s.listen(1)
 
while 1:
    try:
        clientsock, clientaddr = s.accept()
        print "Baglanan: ", clientsock.getpeername()
        while 1:
            data = clientsock.recv(4096)
            if not len(data):
                break
            clientsock.sendall(data)
        clientsock.close()
    except KeyboardInterrupt:
        raise
    except:
        traceback.print_exc()
        continue