python會話 python會話?

2021-10-12 10:58:01 字數 4979 閱讀 6511

這可能是乙個老問題,但我面臨著同樣的問題:python中沒有內建的會話支援。我編寫了乙個簡單的會話處理程式,可能對某些人有用:#!/usr/bin/python

# -*- coding: utf-8 -*-

import tempfile

import hashlib

import os

import time

import random

import errno

import ast

import pprint

import fcntl

class cgisession:

__sessionfile = ''

__newid = ''

__date = ''

__tmp = ''

__pid = ''

__time = ''

__rand = ''

__pp = pprint.prettyprinter()

__fs = ''

def __init__(

self,

id=none,

directory=none,

hash='md5',

if directory is none:

self.__directory = tempfile.gettempdir()

else:

if not os.path.exists(directory):

try:

os.makedirs(directory)

except oserror, error:

if error.errno != errno.eexist:

raise

self.__directory = directory

if hash not in ['md5', 'sha256', 'sha512']:

self.__hash = 'md5'

else:

self.__hash = hash

if id is none:

self.__id = self.__gensessionid()

self.__fs = open(os.path.join(self.__directory,

'pycgiession_'.format(self.__id)), 'w')

fcntl.flock(self.__fs.fileno(), fcntl.lock_ex)

self.__fs.write('{}')

self.__fs.close()

self.__sessionfile = os.path.join(self.__directory,

'pycgiession_'.format(self.__id))

os.chmod(self.__sessionfile, 0640)

else:

try:

open(os.path.join(self.__directory,

'pycgiession_'.format(os.path.basename(id))),

'r').close()

self.__id = os.path.basename(id)

self.__sessionfile = os.path.join(self.__directory,

'pycgiession_'.format(self.__id))

os.chmod(self.__sessionfile, 0640)

except ioerror, error:

if error.errno == errno.enoent:

self.__id = self.__gensessionid()

self.__fs = open(os.path.join(self.__directory,

'pycgiession_'.format(self.__id)), 'w')

fcntl.flock(self.__fs.fileno(), fcntl.lock_ex)

self.__fs.write('{}')

self.__fs.close()

self.__sessionfile = os.path.join(self.__directory,

'pycgiession_'.format(self.__id))

os.chmod(self.__sessionfile, 0640)

else:

raise

def __gensessionid(self):

self.__pid = str(os.getpid())

self.__time = ''.join(map(str, time.gmtime()[0:]))

self.__rand = str(random.random())

if self.__hash == 'sha256':

sha256 = hashlib.sha256()

sha256.update(self.__pid)

sha256.update(self.__time)

sha256.update(self.__rand)

self.__newid = sha256.hexdigest()

elif self.__hash == 'sha512':

sha512 = hashlib.sha512()

sha512.update(self.__pid)

sha512.update(self.__time)

sha512.update(self.__rand)

self.__newid = sha512.hexdigest()

else:

md5 = hashlib.md5()

md5.update(self.__pid)

md5.update(self.__time)

md5.update(self.__rand)

self.__newid = md5.hexdigest()

return self.__newid

def getid(self):

return self.__id

def setparam(self, key, value):

self.__fs = open(self.__sessionfile, 'r')

fcntl.flock(self.__fs.fileno(), fcntl.lock_ex)

self.__date = self.__fs.readlines()

self.__fs.close()

self.__fs = open(self.__sessionfile, 'w')

fcntl.flock(self.__fs.fileno(), fcntl.lock_ex)

self.__date = ast.literal_eval(self.__date[0])

self.__date[key] = value

self.__fs.write(self.__pp.pformat(self.__date))

self.__fs.close()

def getparam(self, key):

self.__fs = open(self.__sessionfile, 'r')

fcntl.flock(self.__fs.fileno(), fcntl.lock_ex)

self.__date = self.__fs.readline()

self.__fs.close()

self.__date = ast.literal_eval(self.__date)

try:

self.__date = self.__date[key]

return self.__date

except keyerror:

return none

def getall(self):

self.__fs = open(self.__sessionfile, 'r')

fcntl.flock(self.__fs.fileno(), fcntl.lock_ex)

self.__date = self.__fs.readlines()

self.__fs.close()

self.__date = ast.literal_eval(self.__date[0])

return self.__date

def delparam(self, key):

self.__fs = open(self.__sessionfile, 'r')

fcntl.flock(self.__fs.fileno(), fcntl.lock_ex)

self.__date = self.__fs.readlines()

self.__fs.close()

self.__fs = open(self.__sessionfile, 'w')

fcntl.flock(self.__fs.fileno(), fcntl.lock_ex)

self.__date = ast.literal_eval(self.__date[0])

try:

del self.__date[key]

except keyerror:

pass

self.__fs.write(self.__pp.pformat(self.__date))

self.__fs.close()

def clear(self):

self.__fs = open(self.__sessionfile, 'w')

fcntl.flock(self.__fs.fileno(), fcntl.lock_ex)

self.__fs.write('{}')

self.__fs.close()

def delete(self):

os.remove(self.__sessionfile)

示例:^$

python會話 Python在互動式會話中登入

我試圖在python 2.7應用程式中實現日誌記錄,並發現它非常有用。但是,我注意到,在互動式執行python時,每個日誌訊息都會多次列印。訊息的列印次數與先前執行指令碼的次數相同,因此似乎記錄器未在指令碼末尾正確清理 我猜測 考慮下面的例子 import sys import logging de...

Python終端會話是什麼

終端是linux裡的叫法,windows下面叫做命令提示符,所有的windows系統直接按你鍵盤上的win r 後,在執行框裡輸入cmd確定就行了。下面演示分別演示windows系統下從終端啟動python,及python的互動模式和命令列模式 終端啟動python win r,在執行框裡輸入cmd...

Python庫總結會

scikit surprise是易於使用的python scikit,適用於推薦系統 gensim scikit learn是簡單有效的資料探勘和資料分析工具 whoosh是快速,純粹的python搜尋引擎庫 recsys是提供了構建和評估許多推薦系統 演算法效能的工具 lenskit是一組用於試驗...