Python/CGIスクリプト卸問屋は創業以来4,573,631,826名のお客様にご愛顧いただいております。

創業132年を誇る超老舗のPython/CGIスクリプト卸問屋スクリプト卸問屋へようこそ。当店では.....省略

ファイルのリスト
  1. project/chart.pyソースひらく
  2. project/crash.pyソースひらく
  3. project/obs.pyソースとじる
  4. project/pycalc.pywのソースひらく
project/obs.py
#!/usr/local/bin/python
# -*- coding: cp932 -*-

bbsmenu_url      = 'http://menu.2ch.net/bbsmenu.html'
bbsmenu_file     = './obs_bbsmenu.dat'
status_file      = './obs_status.dat'
ping_interval    = 10 * 60
bbsmenu_interval = 6 * 60 * 60
max_threads      = 4
color            = ['red', 'green']
status           = [
    '<img src="failure.gif" alt="(´・ω・`)ショボーン">',
    '<img src="success.gif" alt="(`・ω・´)シャキーン">'
]

# Unix => fcntl, Windows => msvcrt
# と言うことでロックは省略しました。
# 設定おわり

import sys
import os
import socket
import httplib
import threading
import time
import re
import warnings
#import cgi

def parse_bbsmenu(html):
    data = []
    tmp = {}
    for line in html.split("\n"):
        obj = re.search('(?i)<a href="?(http://[0-9A-Za-z_]{1,64}\.2ch\.net/)(?:([0-9A-Za-z_]{1,64})/)?"?[^<>]{0,256}>([^<>]{1,256})</a>', line)
        if obj:
            url = obj.group(1)
            path = obj.group(2)
            board = obj.group(3).replace('&lt;', '<').replace('&gt;', '>')
            if path == None:
                path = ''
            try:
                tmp[url].append([path, board])
            except:
                data.append([url, []])
                tmp[url] = [[path, board]]
    for i in xrange(0, len(data)):
        data[i][1] = tmp.get(data[i][0])
    return data

def read_bbsmenu(filename):
    data = []
    try:
        i = -1
        fp = file(filename, 'r')
        for line in fp:
            value = line.rstrip("\n").split("\t")
            if len(value) == 1 and re.match('^[A-Za-z]{1,64}://([0-9A-Za-z_.-]{1,256})(?::(\d{1,5}))?', value[0]):
                i += 1
                data.append([value[0], []])
            elif len(value) == 3:
                data[i][1].append([value[1], value[2]])
        fp.close()
    except:
        warnings.warn('read_bbsmenu()')
    return data

def write_bbsmenu(filename, data):
    result = None
    try:
        fp = file(filename, 'w')
        result = 0
        for i in data:
            fp.write(i[0] + "\n")
            for j in i[1]:
                fp.write("\t" + j[0] + "\t" + j[1] + "\n")
        result = fp.tell()
        fp.close()
    except:
        warnings.warn('write_bbsmenu()')
    return result

def read_status(filename):
    data = {}
    try:
        fp = file(filename, 'r')
        for line in fp:
            value = line.rstrip("\n").split("\t")
            if len(value) == 2:
                data[value[0]] = int(value[1])
        fp.close()
    except:
        warnings.warn('read_status()')
    return data

def write_status(filename, data):
    result = None
    try:
        fp = file(filename, 'w')
        result = 0
        for k, v in data.iteritems():
            fp.write(k + "\t" + str(v) + "\n")
        result = fp.tell()
        fp.close()
    except:
        warnings.warn('write_status()')
    return result

def get_html(url):
    buf = 65536
    proto, host = url.split('://')
    path = '/'
    pos = host.find('/')
    html = ''
    if 0 <= pos:
        path = host[pos:]
        host = host[:pos]
    try:
        conn = httplib.HTTPConnection(host)
        conn.request('GET', path)
        html = conn.getresponse().read(buf)
    except:
        warnings.warn('get_html()')
    return html

#def _ping(fd, lock, host, port = 80):
#    status = 0
#    try:
#        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
#        s.connect((host, port))
##        data = s.send("GET / HTTP/1.0\nHost: " + host + "\n\n\n")
##        print s.recv(1024)
#        s.close()
#        status = 1
#    except:
#        pass
#    lock.acquire()
#    os.write(fd, host + ':' + str(port) + "\t" + str(status) + "\n")
#    lock.release()
#
#def ping(url_list, max_threads = 1):
#    if type(url_list) == type(''):
#        url_list = [url_list]
#    if max_threads < 1:
#        max_threads = 1
#    lock = threading.Lock()
#    rfd, wfd = os.pipe()
#    for url in url_list:
#        obj = re.search('://([0-9A-Za-z_.-]{1,256})(?::(\d{1,5}))?', url)
#        if obj:
#            host, port = obj.group(1), obj.group(2)
#            try:
#                port = int(port)
#                if not 0 <= port < 65536:
#                    port = 80
#            except:
#                port = 80
#            while max_threads < threading.activeCount():
#                time.sleep(0.001)
#            try:
#                threading.Thread(None, _ping, None, (wfd, lock, host, port)).start()
#            except:
#                warnings.warn('threading.Thread()')
#    while 1 < threading.activeCount():
#        time.sleep(0.001)
#    os.close(wfd)
#    text = os.read(rfd, os.fstat(rfd).st_size)
#    os.close(rfd)
#    data = {}
#    for line in text.split("\n"):
#        value = line.split("\t")
#        if len(value) == 2:
#            data[value[0]] = int(value[1])
#    return data

def _ping(fp, lock, host, port = 80):
    status = 0
    try:
        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        s.connect((host, port))
#        data = s.send("GET / HTTP/1.0\nHost: " + host + "\n\n\n")
#        print s.recv(1024)
        s.close()
        status = 1
    except:
        pass
    lock.acquire()
    fp.write(host + ':' + str(port) + "\t" + str(status) + "\n")
    lock.release()

def ping(filename, url_list, max_threads = 1):
    if type(url_list) == type(''):
        url_list = [url_list]
    if max_threads < 1:
        max_threads = 1
    lock = threading.Lock()
    fp = file(filename, 'w+')
    for url in url_list:
        obj = re.search('://([0-9A-Za-z_.-]{1,256})(?::(\d{1,5}))?', url)
        if obj:
            host, port = obj.group(1), obj.group(2)
            try:
                port = int(port)
                if not 0 <= port < 65536:
                    port = 80
            except:
                port = 80
            while max_threads < threading.activeCount():
                time.sleep(0.001)
            try:
                threading.Thread(None, _ping, None, (fp, lock, host, port)).start()
            except:
                warnings.warn('threading.Thread()')
    while 1 < threading.activeCount():
        time.sleep(0.001)
    fp.flush()
    fp.seek(0)
    data = {}
    for line in fp:
        value = line.rstrip("\n").split("\t")
        if len(value) == 2:
            data[value[0]] = int(value[1])
    return data


bbsmenu_data = read_bbsmenu(bbsmenu_file)
if 0 < bbsmenu_interval and (not len(bbsmenu_data) or os.stat(bbsmenu_file).st_mtime + bbsmenu_interval < time.time()):
    bbsmenu_data = parse_bbsmenu(get_html(bbsmenu_url))
    write_bbsmenu(bbsmenu_file, bbsmenu_data)

status_data = read_status(status_file)
if 0 < ping_interval and (not len(status_data) or os.stat(status_file).st_mtime + ping_interval < time.time()):
#    status_data = ping([url for url, path in bbsmenu_data], max_threads)
#    write_status(status_file, status_data)
    status_data = ping(status_file, [url for url, path in bbsmenu_data], max_threads)

print 'Content-Type: text/html'
print '''
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html lang="ja">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=Shift_JIS">
<meta http-equiv="Content-Style-Type" content="text/css">
<style type="text/css">
<!--
a { font-size:90% }
a:hover { border:1px solid #ffa500; color:green; font-weight:bold; text-decoration:none; }
ol { margin:0px; padding:0px; }
ol li { display:inline }
dd { padding-bottom:5px }
-->
</style>
<title>2ch 観測所</title>
</head>
<body>
<dl>
'''
for url, bbs in bbsmenu_data:
    key = re.sub('^.+?://([^/]+).*$', '\\1', url)
    if key.find(':') < 0:
        key += ':80'
    print '<dt style="color:' + color[status_data.get(key, 0)] + '">' + status[status_data.get(key, 0)] + ' <strong>' + key + '</strong>'
    if bbs:
        print '(%d)' % len(bbs)
    print '<dd>'
    print '<ol>'
    for path, board in bbs:
        if len(path):
            path += '/'
        print '<li><a href="' + url + path + '">' + board.replace('<', '&lt;').replace('>', '&gt;') + '</a>'
    print '</ol>'
print '</dl>'
print '<ul>'
if 0 < ping_interval:
    print '<li>サーバー情報の更新間隔は %d秒' % ping_interval
if 0 < bbsmenu_interval:
    print '<li>bbsmenu の更新間隔は %d秒' % bbsmenu_interval
print '</ul>'
print '</body>'
print '</html>'

sys.exit()
注意事項
メールで連絡
ちょこっとテスト
となりの問屋

©2018 Python/CGIスクリプト卸問屋