Skip to content

Instantly share code, notes, and snippets.

@oska874
Created October 26, 2015 01:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oska874/e1deca4f2361c0daf40f to your computer and use it in GitHub Desktop.
Save oska874/e1deca4f2361c0daf40f to your computer and use it in GitHub Desktop.
import Tkinter as tk
import tkFileDialog
import transmit_cli
## callback
def selectTftpFold():
global tfoldStr
dir = tkFileDialog.askdirectory()
if dir != "" :
tfoldStr.set(dir)
else:
tfoldStr.set(u"./")
def selectFtpFold():
global ffoldStr
dir = tkFileDialog.askdirectory()
if dir != "" :
ffoldStr.set(dir)
else:
ffoldStr.set(u"./")
def selectHttpFold():
global hfoldStr
dir = tkFileDialog.askdirectory()
if dir != "" :
hfoldStr.set(dir)
else:
hfoldStr.set(u"./")
def selectSocketFold():
global sfoldStr
dir = tkFileDialog.askdirectory()
if dir != "":
sfoldStr.set(dir)
else:
sfoldStr.set(u"./")
###
# tftp
def tftpdStartCall():
global tfoldStr
transmit_cli.my_tftpd(ip=tfipStr.get(), port=tfportStr.get(),local=tfoldStr.get())
def tftpdStopCall():
transmit_cli.my_tftpd2()
# ftp
def ftpdStartCall():
global ffoldStr
transmit_cli.my_ftpd(ip=fipStr.get(), port=fportStr.get(),local=ffoldStr.get())
print ffoldStr.get()
def ftpdStopCall():
transmit_cli.my_ftpd2()
#http
def httpdStartCall():
global hfoldStr
transmit_cli.my_httpd(ip=hipStr.get(), port=hportStr.get(),local=u"./")#hfoldStr.get())
def httpdStopCall():
transmit_cli.my_httpd2()
#socket
def socketdCall():
global sfoldStr
transmit_cli.my_socketd(ip=sipStr.get(), port=sportStr.get(),local=sfoldStr.get())
## main ##
if __name__ == "__main__":
root = tk.Tk()
#value of entris
fipStr=tk.StringVar()
fportStr=tk.StringVar()
ffoldStr=tk.StringVar()
tfipStr=tk.StringVar()
tfportStr=tk.StringVar()
tfoldStr=tk.StringVar()
hipStr=tk.StringVar()
hportStr=tk.StringVar()
hfoldStr=tk.StringVar()
sipStr=tk.StringVar()
sportStr=tk.StringVar()
sfoldStr=tk.StringVar()
#ftp
ftpPane = tk.PanedWindow(orient=tk.HORIZONTAL)
ftpPane.pack()
ftpIp = tk.Entry(ftpPane, bd =5,textvariable=fipStr)
ftpIp.insert(0,"0.0.0.0")
ftpPane.add(ftpIp)
ftpPort = tk.Entry(ftpPane, bd =5,textvariable=fportStr)
ftpPort.insert(0,"20")
ftpPane.add(ftpPort)
ftpFold = tk.Entry(ftpPane, bd =5,textvariable=ffoldStr)
ftpFold.insert(0,"./")
ftpPane.add(ftpFold)
ftpBtnSel = tk.Button(text="select fold",width=10,command=selectFtpFold)
ftpPane.add(ftpBtnSel)
ftpBtnStart = tk.Button(text="ftp start",width=10,command=ftpdStartCall)
ftpPane.add(ftpBtnStart)
ftpBtnStop = tk.Button(text="stop",width=10,command=ftpdStopCall)
ftpPane.add(ftpBtnStop)
#tftp
tftpPane = tk.PanedWindow(orient=tk.HORIZONTAL)
tftpPane.pack()
tftpIp = tk.Entry(tftpPane, bd =5,textvariable=tfipStr)
tftpIp.insert(0,"0.0.0.0")
tftpPane.add(tftpIp)
tftpPort = tk.Entry(tftpPane, bd =5,textvariable=tfportStr)
tftpPort.insert(0,"69")
tftpPane.add(tftpPort)
tftpFold = tk.Entry(tftpPane, bd =5,textvariable=tfoldStr)
tftpFold.insert(0,"./")
tftpPane.add(tftpFold)
tftpBtnSel = tk.Button(text="select fold",width=10,command=selectTftpFold)
tftpPane.add(tftpBtnSel)
tftpBtnStart = tk.Button(text="tftp start",width=10,command=tftpdStartCall)
tftpPane.add(tftpBtnStart)
tftpBtnStop = tk.Button(text="stop",width=10,command=tftpdStopCall)
tftpPane.add(tftpBtnStop)
#http
httpPane = tk.PanedWindow(orient=tk.HORIZONTAL)
httpPane.pack()
httpIp = tk.Entry(httpPane, bd =5,textvariable=hipStr)
httpIp.insert(0,"0.0.0.0")
httpPane.add(httpIp)
httpPort = tk.Entry(httpPane, bd =5,textvariable=hportStr)
httpPort.insert(0,"80")
httpPane.add(httpPort)
httpFold = tk.Entry(httpPane, bd =5,textvariable=hfoldStr)
httpFold.insert(0,"no use")
httpPane.add(httpFold)
httpBtnSel = tk.Button(text="select fold",width=10,command=selectHttpFold)
httpPane.add(httpBtnSel)
httpBtn = tk.Button(text="http start",width=10,command=httpdStartCall)
httpPane.add(httpBtn)
httpBtnStop = tk.Button(text="stop",width=10,command=httpdStopCall)
httpPane.add(httpBtnStop)
#socket
socketPane = tk.PanedWindow(orient=tk.HORIZONTAL)
socketPane.pack()
socketIp = tk.Entry(socketPane, bd =5,textvariable=sipStr)
socketIp.insert(0,"0.0.0.0")
socketPane.add(socketIp)
socketPort = tk.Entry(socketPane, bd =5,textvariable=sportStr)
socketPort.insert(0,"1080")
socketPane.add(socketPort)
socketFold = tk.Entry(socketPane, bd =5,textvariable=sfoldStr)
socketFold.insert(0,"./")
socketPane.add(socketFold)
socketBtnSel = tk.Button(text="select fold",width=10,command=selectSocketFold)
socketPane.add(socketBtnSel)
socketBtn = tk.Button(text="socket start",width=10,command=socketdCall)
socketPane.add(socketBtn)
socketBtnStop = tk.Button(text="stop",width=10,command=socketdCall)
socketPane.add(socketBtnStop)
## info
infoPane = tk.PanedWindow(orient=tk.HORIZONTAL)
infoPane.pack()
root.mainloop()
import tftpy as tftp
import re
import multiprocessing as mp
## ftp
from pyftpdlib.authorizers import DummyAuthorizer
from pyftpdlib.handlers import FTPHandler
from pyftpdlib.servers import FTPServer
from pyftpdlib.filesystems import AbstractedFS
## http
import sys
import BaseHTTPServer
from SimpleHTTPServer import SimpleHTTPRequestHandler
## global var ##
tftpdOn = 0
tftpSer = 0
tftpProcess = 0
ftpdOn = 0
ftpSer = 0
ftpProcess = 0
httpdOn = 0
socketdOn = 0
processAll = {}
## comman func ##
def valid_ip(ip):
ipRe = re.compile("([0-9]{1,3}\.){3}[0-9]{1,3}$") #basic rule,and need improvement
if ipRe.match(ip) != None:
#print("get ip: "+ip)
return 0
else:
return -1
def valid_port(port):
portRe = re.compile("[0-9]+$")
if portRe.match(port) != None:
#print("get port : "+port)
return 0
else:
return -1
def stop_proc(name):
global processAll
processAll[name].terminate()
def start_transd(type,ip,port,local):
global tftpSer
if type == "tftp":
tftpSer = tftp.TftpServer(local)
tftpSer.listen(ip,int(port))
elif type == "ftp":
# Instantiate a dummy authorizer for managing 'virtual' users
authorizer = DummyAuthorizer()
# Define a new user having full r/w permissions
authorizer.add_user('user_name', 'pass_word','./', perm='elradfmwM',msg_login='welcome',msg_quit='bye')
# Define a read-only anonymous user
authorizer.add_anonymous(local)
# Instantiate FTP handler class
handler = FTPHandler
handler.authorizer = authorizer
handler.max_login_attempts = 3
handler.permit_foreign_addresses = True
handler.tcp_no_delay = True
# Define a customized banner (string returned when client connects)
handler.banner = "Welcome to my FTP."
# Instantiate FTP server class and listen on 127.0.0.1:21
address = (ip, int(port))
server = FTPServer(address, handler)
# set a limit for connections
server.max_cons = 128
server.max_cons_per_ip = 2
absfs = AbstractedFS(unicode(local),handler)
#absfs.cwd = u"/bbb/ss/"
# start ftp server
server.serve_forever()
elif type == "http":
HandlerClass = SimpleHTTPRequestHandler
ServerClass = BaseHTTPServer.HTTPServer
Protocol = "HTTP/1.0"
server_address = (ip,int(port))
HandlerClass.protocol_version = Protocol
httpd = ServerClass(server_address, HandlerClass)
sa = httpd.socket.getsockname()
print "Serving HTTP on", sa[0], "port", sa[1], "..."
httpd.serve_forever()
## tftp set##
def my_tftpd(ip=0,port=0,local="./"):
global tftpdOn
global tftpProcess
global processAll
if tftpdOn == 0:
tftpdOn = 2
if valid_port(port) == 0 and valid_ip(ip) == 0:
tftpProcess = mp.Process(name='tftpS', target=start_transd,args=("tftp",ip,port,local))
tftpProcess.start()
processAll['tftpS']=tftpProcess
return 0
else:
return -1
def my_tftpd2():
global tftpdOn
global tftpSer
global tftpProcess
if tftpdOn == 2:
tftpdOn = 0
stop_proc("tftpS")
return 0
## ftp set ##
def my_ftpd(ip=0,port=0,local="./"):
global ftpdOn
global ftpProcess
global processAll
if ftpdOn == 0:
print("111: "+local)
ftpdOn = 2
if valid_port(port) == 0 and valid_ip(ip) == 0:
ftpProcess = mp.Process(name='ftpS', target=start_transd,args=("ftp",ip,port,local))
ftpProcess.start()
processAll['ftpS']=ftpProcess
return 0
else:
return -1
def my_ftpd2():
global ftpdOn
global ftpProcess
if ftpdOn == 2:
ftpdOn = 0
stop_proc("ftpS")
return 0
## http set ##
def my_httpd(ip=0,port=0,local="./"):
global httpdOn
global httpProcess
global processAll
if httpdOn == 0:
httpdOn = 2
if valid_port(port) == 0 and valid_ip(ip) == 0:
httpProcess = mp.Process(name='httpS', target=start_transd,args=("http",ip,port,local))
httpProcess.start()
processAll['httpS']=httpProcess
return 0
else:
return -1
def my_httpd2():
global httpdOn
global httpProcess
if httpdOn == 2:
httpdOn = 0
stop_proc("httpS")
return 0
## socket set ##
def my_socketd(ip=0,port=0,local="./"):
if valid_port(port) == 0 and valid_ip(ip) == 0:
print("start socketd : "+ip+":"+port)
return 0
else:
return -1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment