コード例 #1
0
ファイル: socket_process.py プロジェクト: hyqgod/Barrage
def generate_socket(room_id):
    """Generate the socket to communicate with Bilibili Danmaku Server.

    :param room_id: the id of live room.
    """
    is_first = True
    print "请求服务器连接"
    retry_time = 0
    socket.setdefaulttimeout(TIME_OUT)
    data = RECIEVE_INIT_DATA % room_id
    send_data = unhexlify(data)
    while True:
        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        try:
            sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            sock.connect((RECIEVE_SERVER_ADDRESS, RECIEVE_SERVER_PORT))
            sock.sendall(send_data)
        except socket.error:
            if retry_time == MAX_RETRY:
                if not is_first:
                    terminate()
                raise RuntimeError("重试请求过多,服务中止!")
            print "服务器连接失败..."
            retry_time += 1
            time.sleep(4)
            continue

        if is_first:
            print "开始接收弹幕。(Ctrl + C 退出)"
            is_first = False
        retry_time = 0
        try:
            yield sock
        finally:
            sock.close()
コード例 #2
0
def generate_socket(room_id):
    """Generate the socket to communicate with Bilibili Danmaku Server.

    :param room_id: the id of live room.
    """
    global is_first
    retry_time = 0
    socket.setdefaulttimeout(TIME_OUT)
    userid = int(100000000 * random.random())
    body = ('{"roomid": ' + str(room_id) + ', "uid": ' + str(userid) + '}')
    while True:
        try:
            sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            address = get_server(room_id)
            sock.connect((address, 788))
            send_data = send_socket_data(sock, 16 + len(body), 16, 1, 7, 1,
                                         body)
        except socket.error as exc:
            if retry_time == MAX_RETRY:
                if not is_first:
                    terminate()
                raise RuntimeError("重试请求过多,服务中止!")
            print "服务器连接失败..."
            retry_time += 1
            time.sleep(4)
            continue

        if is_first:
            print "开始接收弹幕。(Ctrl + C 退出)"
            is_first = False
        retry_time = 0
        try:
            yield sock
        finally:
            sock.close()
コード例 #3
0
ファイル: reciever.py プロジェクト: chaserhkj/Barrage
 def __init__(self, room_id):
     """初始化服务。
     :params: room_id: 直播间号。
     """
     socket.setdefaulttimeout(20)
     data = RECIEVE_INIT_DATA % room_id
     self.send_data = unhexlify(data)
     self.danmaku_queue = DanmakuQueue(room_id)
     self.is_terminate = False
     self.socket = None
コード例 #4
0
ファイル: socket_process.py プロジェクト: haozi23333/Barrage
def generate_socket(room_id):
    """Generate the socket to communicate with Bilibili Danmaku Server.

    :param room_id: the id of live room.
    """
    is_first = True
    print "请求服务器连接"
    retry_time = 0
    socket.setdefaulttimeout(TIME_OUT)
    data = RECIEVE_INIT_DATA % room_id
    send_data = unhexlify(data)
    while True:
        sock = socket.socket(socket.AF_INET,
                             socket.SOCK_STREAM)
        try:
            sock = socket.socket(socket.AF_INET,
                                 socket.SOCK_STREAM)
            sock.connect(
                (RECIEVE_SERVER_ADDRESS, RECIEVE_SERVER_PORT))
            sock.sendall(send_data)
        except socket.error:
            if retry_time == MAX_RETRY:
                if not is_first:
                    terminate()
                raise RuntimeError("重试请求过多,服务中止!")
            print "服务器连接失败..."
            retry_time += 1
            time.sleep(4)
            continue

        if is_first:
            print "开始接收弹幕。(Ctrl + C 退出)"
            is_first = False
        retry_time = 0
        try:
            yield sock
        finally:
            sock.close()
コード例 #5
0
ファイル: socket_process.py プロジェクト: OctavianLee/Barrage
def generate_socket(room_id):
    """Generate the socket to communicate with Bilibili Danmaku Server.

    :param room_id: the id of live room.
    """
    global is_first
    retry_time = 0
    socket.setdefaulttimeout(TIME_OUT)
    userid = int(100000000 * random.random())
    body = ('{"roomid": ' + str(room_id) + ', "uid": ' + str(userid) +'}')
    while True:
        try:
            sock = socket.socket(socket.AF_INET,
                             socket.SOCK_STREAM)
            address = get_server(room_id)
            sock.connect(
                (address, 788))
            send_data = send_socket_data(sock, 16 + len(body), 16,
                    1, 7, 1, body)
        except socket.error as exc:
            if retry_time == MAX_RETRY:
                if not is_first:
                    terminate()
                raise RuntimeError("重试请求过多,服务中止!")
            print "服务器连接失败..."
            retry_time += 1
            time.sleep(4)
            continue

        if is_first:
            print "开始接收弹幕。(Ctrl + C 退出)"
            is_first = False
        retry_time = 0
        try:
            yield sock
        finally:
            sock.close()
コード例 #6
0
ファイル: hello.py プロジェクト: skey/hello
def main():
    parser = OptionParser(usage='%prog --file=urlfile --limit=1000 --timeout=5')
    parser.add_option('', '--file', help='host:port file')
    parser.add_option('', '--limit', default=10, type="int", help='limit concurrency, default: %default')
    parser.add_option('', '--timeout',  default=5, type="int", help='connect timeout, default: %default')
    (options, args) = parser.parse_args()
    if not options.file:
        parser.print_help()
        exit(0)
    socket.setdefaulttimeout(options.timeout)
    groups = gen_groups([line.strip() for line in open(options.file) if line], options.limit)
    for group in groups:
        jobs = []
        for item in group:
            if item:
                addr, port = item.split(":")
                jobs.append(gevent.spawn(get_connected, addr, int(port)))
        gevent.joinall(jobs)
        for job in jobs:
            addr, port, connected = job.value
            if connected:
                print "%s:%d\tOK" % (addr, port)
            else:
                print "%s:%d\tERROR" % (addr, port)
コード例 #7
0
ファイル: urlexpand.py プロジェクト: srtalbot/dmi-tcat
from gevent.timeout import Timeout
import urlparse
import sys
from random import shuffle
import MySQLdb

from gevent import monkey
monkey.patch_all(thread=False)

#import umysql
import urllib2
from urllib2 import HTTPError, URLError

# set socket timeout in seconds
timeout = 7
socket.setdefaulttimeout(timeout)

#conn = umysql.Connection()

db = MySQLdb.connect(host='localhost', user='', passwd='', db="twittercapture")
cursor = db.cursor()

finished = 0
pool = Pool(50)
updates = []


def get_twitter_tables(table=None):
    if table is not None:
        query = "SHOW TABLES LIKE '%s'" % table
    else:
コード例 #8
0
ファイル: tcp_server.py プロジェクト: stamaimer/UServer
import json
import struct

import gevent

from binascii import hexlify
from datetime import datetime

from gevent.server import StreamServer
from gevent import socket

sys.path.append("../")

from userver import *

socket.setdefaulttimeout(5)

greenlets = {}
"""list: 全局变量`greenlets`

存储设备和设备对应的链接,用来分辨是否是新设备

"""


def translate(data, flag="server"):
    """把一个十六进制字符串以两位为整体进行切割,同时把切割结果转换成为十进制整数

    :param data: 十六进制字符串
    :param flag: 由于程序产生字符串,而设备发送二进制串,需要使用标志区分数据来源,当数据来自设备,需要先使用`binascii.hexlify`进行格式转换.取值范围:{"server"(默认值), "client"}
    :return: 返回一个整数列表
コード例 #9
0
from os.path import abspath

try:
    from newsreap.SocketBase import SocketBase
    from newsreap.SocketBase import SocketException
    from newsreap.SocketBase import DEFAULT_BIND_ADDR

except ImportError:
    sys.path.insert(0, dirname(dirname(abspath(__file__))))
    from newsreap.SocketBase import SocketBase
    from newsreap.SocketBase import SocketException
    from newsreap.SocketBase import DEFAULT_BIND_ADDR

# Our internal server is only used for testing, therefore we can get away with
# having a really low timeout
socket.setdefaulttimeout(10.0)

# The directory containing all of the variable data used
# for the NNTPConnection Testing
NNTP_TEST_VAR_PATH = join(dirname(abspath(__file__)), 'var')

# Empty File
DEFAULT_EMPTY_FILE = join(NNTP_TEST_VAR_PATH, 'emptyfile.msg')

# Article ID
ARTICLE_ID_RE = re.compile(r'\s*<*\s*(?P<id>[^>]+)>?.*')

# All of the default NNTP Responses are defined here by their
# compiled regular expression
NNTP_DEFAULT_MAP = {
    re.compile('AUTHINFO USER valid'): {
コード例 #10
0
    def listen(self, timeout=None, retry_wait=1.00, reuse_port=True):
        """
           input Parameters:
           -timeout     How long accept() should block for before giving up and
                        moving on to one of the retires

           -retry_wait  How many seconds to wait before trying again on a
                        timeout (only applicable if retries is specified)

           output Parameters:
           -True if connection established, False otherwise

           Description:
           establish connection according to object attributes.
           return established connection as self.socket.

        """

        if reuse_port and self._local_port is not None and self.port == 0:
            # Re-use the last port we acquired that way we can close a
            # connection gracefully and not have to re-acquire a new
            # ephemeral port
            self.port = self._local_port

        # Blocking until a connection
        logger.debug("Listening for a connection at: %s:%d" % (
            self.bindaddr,
            self.port,
        ))
        if timeout:
            socket.setdefaulttimeout(timeout)
            logger.debug("Socket timeout set to :%ds" % (timeout))

        # Bind to address
        if not self.bind(timeout=timeout, retries=3):
            return False

        # Never use blocking
        self.socket.setblocking(False)

        # Listen Enabled, the 1 identifies the number of connections we
        # will accept; never handle more then 1 at a time.
        self.socket.listen(1)

        # Store local details of our socket
        (self._local_addr, self._local_port) = self.socket.getsockname()

        # Get reference time
        cur_time = datetime.now()
        while True:
            try:
                conn, (self._remote_addr, self._remote_port) = \
                    self.socket.accept()
                # If we get here, we've got a connection
                break

            except TypeError, e:
                # Timeout occurred
                pass

            except AttributeError, e:
                # Usually means someone called close() while accept() was
                # blocked. Happens when using this class with threads.
                # No problem... we'll just finish up here
                self.close()
                raise SocketException('Connection broken abruptly')
コード例 #11
0
from platform import python_version

__author__ = "Sanjay Joshi"
__copyright__ = "IBM Copyright 2015"
__credits__ = ["Sanjay Joshi"]
__license__ = "Apache 2.0"
__version__ = "1.0"
__maintainer__ = "Sanjay Joshi"
__email__ = "*****@*****.**"
__status__ = "Prototype"

# Monkey Patching app behavior to make it greenlet non-blocking
# This is usually required by gevent for native bindings for things
# like Redis interactions, etc ...
monkey.patch_all()
socket.setdefaulttimeout(240)
# capture current working directory
PWD = os.environ.get("PWD")
# set static folder path for static data
static_folder = os.path.join(PWD, "macreduce/static")

# Detect if we are deployed within Bluemix or not and configure accordingly
if VCAP_CONFIG:
    print('Welcome to Bluemix')
    print('Running on Python version: ' + python_version())
    app = Eve(static_folder=static_folder, redis=REDIS_INSTANCE)
    REDIS_INSTANCE.flushdb()

else:
    print('We are not running in Bluemix! Dev Mode Enabled')
    app = Eve(static_folder=static_folder, redis=REDIS_INSTANCE)
コード例 #12
0
from platform import python_version

__author__ = "Sanjay Joshi"
__copyright__ = "IBM Copyright 2015"
__credits__ = ["Sanjay Joshi"]
__license__ = "Apache 2.0"
__version__ = "1.0"
__maintainer__ = "Sanjay Joshi"
__email__ = "*****@*****.**"
__status__ = "Prototype"

# Monkey Patching app behavior to make it greenlet non-blocking
# This is usually required by gevent for native bindings for things
# like Redis interactions, etc ...
monkey.patch_all()
socket.setdefaulttimeout(240)
# capture current working directory
PWD = os.environ.get("PWD")
# set static folder path for static data
static_folder = os.path.join(PWD, "macreduce/static")

# Detect if we are deployed within Bluemix or not and configure accordingly
if VCAP_CONFIG:
    print("Welcome to Bluemix")
    print("Running on Python version: " + python_version())
    app = Eve(static_folder=static_folder, redis=REDIS_INSTANCE)
    REDIS_INSTANCE.flushdb()

else:
    print("We are not running in Bluemix! Dev Mode Enabled")
    app = Eve(static_folder=static_folder, redis=REDIS_INSTANCE)
コード例 #13
0
ファイル: urlexpand.py プロジェクト: ygubanina/dmi-tcat
import sys
from random import shuffle
import MySQLdb
import time
from collections import deque
import re
import os.path

from gevent import monkey
monkey.patch_all(thread=False)

import requests

# set socket timeout in seconds
socket_timeout = 7
socket.setdefaulttimeout(socket_timeout)

db_host = 'localhost'
db_user = '******'
db_passwd = ''
db_db = 'twittercapture'

with open(os.path.dirname(__file__) + '/../config.php', 'r') as f:
    read_data = f.read()
    lines = read_data.split('\n')
    for line in lines:
        result = re.search('^\$dbuser *= *["\'](.*)["\']', line)
        if result:
            db_user = result.group(1)
        result = re.search('^\$dbpass *= *["\'](.*)["\']', line)
        if result:
コード例 #14
0
def get_io_return():

    global test_user_data
    global result_dict

    while True:
        #
        print('begin sleep!')
        time.sleep(8)
        print('wake!')

        try:
            customer_id = test_user_data[0]
            test_user_data.pop(0)

            #时间模块的计算
            now_date, now_time = datetime.datetime.now().strftime(
                '%Y%m%d,%H%M%S').split(',')
            timestamp_end8 = str(datetime.datetime.now().timestamp() *
                                 10**6)[-10:-2]

            str_1 = "{\n" \
                    + "\"transactionType\":\"CustomerIdToUser.Req\",\n" \
                    + "\"branchId\":\"JR001\",\n" \
                    + "\"code\":\"UTF-8\",\n" \
                    + "\"orgTxDate\":\"%s\",\n"%(now_date) \
                    + "\"orgTxTime\":\"%s\",\n"%(now_time) \
                    + "\"orgTxLogNo\":\"%s\",\n"%(timestamp_end8) \
                    + "\"requestMessage\":\n"\
                    + "{\n" \
                    + "\"customerId\":\"%s\"\n"%(customer_id)\
                    + "}\n" \
                    + "}"

            signKey = "bugaosuni"  #生产

            #获得MD5
            str_f = str_1 + signKey
            md5 = hashlib.md5()
            md5.update(str_f.encode('utf8'))
            sign = md5.hexdigest()

            #获得sign拼接报文的长度,并取bytes
            sd = sign + str_1
            str_len_add8 = len(bytes(sd, encoding='utf8')) + 8
            str_len_add8_bytes = str_len_add8.to_bytes(4, byteorder='big')
            str_bytes = bytes(sd, encoding='utf8')
            will_send = str_len_add8_bytes + str_bytes

            #每次建立socket
            socket_ = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            socket.setdefaulttimeout(1)
            socket_.connect(("334.68.88.15", 95))  #内网生产

            #获得报文信息
            socket_.sendall(will_send)
            get_info_ = socket_.recv(1024)

            get_info = get_info_.decode('gbk')[40:]
            print(get_info)
            #关闭socket
            socket_.close()

            #准备输出
            get_info_json = json.loads(get_info)
            customer_pid = get_info_json['message']['CUSTOMERPID']
            customer_phone = get_info_json['message']['DEUSERID']
            result_dict[customer_id] = customer_phone

        except IndexError as e:
            return 'IndexError Done'
コード例 #15
0
ファイル: urlexpand.py プロジェクト: Sparklet73/dmi-tcat
from gevent.timeout import Timeout
import urlparse
import sys
from random import shuffle
import MySQLdb

from gevent import monkey
monkey.patch_all(thread=False)

#import umysql
import urllib2
from urllib2 import HTTPError, URLError

# set socket timeout in seconds
timeout = 7
socket.setdefaulttimeout(timeout)

#conn = umysql.Connection()

db = MySQLdb.connect(host='localhost', user='', passwd='', db="twittercapture")
cursor = db.cursor()

finished = 0
pool = Pool(50)
updates = []


def get_twitter_tables(table = None):
    if table is not None:
        query = "SHOW TABLES LIKE '%s'" % table
    else:
コード例 #16
0
from os.path import abspath

try:
    from newsreap.SocketBase import SocketBase
    from newsreap.SocketBase import SocketException
    from newsreap.SocketBase import DEFAULT_BIND_ADDR

except ImportError:
    sys.path.insert(0, dirname(dirname(abspath(__file__))))
    from newsreap.SocketBase import SocketBase
    from newsreap.SocketBase import SocketException
    from newsreap.SocketBase import DEFAULT_BIND_ADDR

# Our internal server is only used for testing, therefore we can get away with
# having a really low timeout
socket.setdefaulttimeout(10.0)

# The directory containing all of the variable data used
# for the NNTPConnection Testing
NNTP_TEST_VAR_PATH = join(dirname(abspath(__file__)), 'var')

# Empty File
DEFAULT_EMPTY_FILE = join(NNTP_TEST_VAR_PATH, 'emptyfile.msg')

# Article ID
ARTICLE_ID_RE = re.compile(r'\s*<*\s*(?P<id>[^>]+)>?.*')

# All of the default NNTP Responses are defined here by their
# compiled regular expression
NNTP_DEFAULT_MAP = {
    re.compile('AUTHINFO USER valid'): {
コード例 #17
0
ファイル: __init__.py プロジェクト: RedHatQE/dva
import sys
from gevent import monkey

monkey.patch_all()
from gevent import socket

import dva.connection.gevent_ssl

socket.setdefaulttimeout(3)
コード例 #18
0
import struct
import binascii
from gevent.pool import Pool
from gevent.server import StreamServer
from gevent import socket

__author__ = '*****@*****.**'

# ===Server configuration begin=== #
# server port
SERVER_PORT = 20199
# max tcp connection size(max value 50000)
POOL_SIZE = 1000
# tcp socket timeout(in seconds)
TIMEOUT = 60
socket.setdefaulttimeout(TIMEOUT)
# ===Server configuration end=== #

socket_dict = dict()

unpacker = struct.Struct('!H H f')


def handle(sock, address):
    socket_dict[address] = sock
    print("Connected:<%s:%s>" % address)
    try:
        while True:
            # Just echos whatever it receives
            try:
                data = sock.recv(unpacker.size)
コード例 #19
0
    def connect(self, timeout=None, retry_wait=1.00):
        """
           input Parameters:
           -timeout     How long connect() should block for before giving up
                        and moving on to one of the retires

           -retry_wait  How many seconds to wait before trying again on a
                        timeout (only applicable if retries is specified)

           output Parameters:
           -True if connection established, False otherwise

           Description:
           establish connection according to object attributes.
           return established connection as self.socket.

        """

        # Blocking until a connection
        logger.debug("Connecting to host: %s:%d" % (
            self.host,
            self.port,
        ))

        if timeout:
            socket.setdefaulttimeout(timeout)
            logger.debug("Socket timeout set to :%ds" % (timeout))

        # Get reference time
        cur_time = datetime.now()

        while True:
            if not self.bind(timeout=timeout, retries=3):
                return False

            # Keep alive flag
            self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1)

            try:
                # Simple protocol that returns if good, otherwise
                # it throws an exception,  So the next line is always
                # to break out of the loop (had we not already thrown
                # an exception
                self.socket.connect((
                    socket.gethostbyname(self.host),
                    int(self.port),
                ))

                # Disable Blocking
                self.socket.setblocking(False)

                # Store local details of our socket
                (self._local_addr, self._local_port) = \
                    self.socket.getsockname()
                (self._remote_addr, self._remote_port) = \
                    self.socket.getpeername()

                logger.info(
                    "Connection established to %s:%d" % (
                        self._remote_addr,
                        self._remote_port,
                    )
                )

                if self.secure is False:
                    # A non secure connection; we're done
                    break

                # Encrypt our socket (changing it into an SSLSocket Object)
                self.__encrypt_socket(
                    timeout=timeout,
                    verify=self.verify_cert,
                    server_side=False,
                )

                # If we get here, we were successful in encrypting the
                # connection; so let's go ahead and break out of our
                # connectino loop
                break

            except ssl.SSLError, e:
                # Secure Connection Failed
                self.close()
                logger.debug(
                    "Failed to secure connection using %s / errno=%d" % (
                        SECURE_PROTOCOL_PRIORITY[self.secure_protocol_idx][1],
                        e[0],
                    ),
                )

                if self.secure is True:
                    # Fetch next (but only if nothing was explicitly
                    # specified)
                    self.__ssl_version(try_next=True)
                    continue

                # If we reach here, we had a problem with our secure connection
                # handshaking and we were explicitly told to only use 1 (one)
                # protocol.  Thus there is nothing more to retry.  So throwing
                # a SocketException() is not a good idea.  Instead, we throw
                # a SocketRetryLimit() so it can be handled differently
                # upstream
                raise SocketRetryLimit('There are no protocols left to try.')

            except socket.error, e:
                logger.debug("Socket exception received: %s" % (e))
                if e[0] == errno.EINTR:
                    # Ensure socket is closed
                    self.close()
                    # A Signal was caught,
                    # return so we can handle this
                    raise