抓取股票数据的定时任务怎么写?0+

57,430 views / 2020.02.28 / 12:12 下午

先写个crontab,规律性的执行一个php脚本,比如2分钟一次。脚本中抓取任务执行前加个stockday判断,就可以了。stockday的实现如下:
function stockday(){
$w = date(“w”,time());
if($w==0||$w==6) return false;//周一到周五执行
//开盘时间大家都知道
$checkDayStr = date(‘Y-m-d ‘,time());
$timeBegin1 = strtotime($checkDayStr.”09:20:00″);
$timeEnd1 = strtotime($checkDayStr.”11:30:00″);
$timeBegin2 = strtotime($checkDayStr.”13:00:00″);
$timeEnd2 = strtotime($checkDayStr.”15:02:00″);
$curr_time = time();
if($curr_time >= $timeBegin1 && $curr_time <= $timeEnd1){
return true;
}
if($curr_time >= $timeBegin2 && $curr_time <= $timeEnd2){
return true;
}
return false;
}

Categories: 感悟 Tags:

gost穿透公司内网控制公司服务器0+

76,388 views / 2019.12.09 / 1:01 下午

1. 需要一台外网服务器,在上面执行:
./gost -L=ss2://AEAD_CHACHA20_POLY1305:password@:443 -L=auto://127.0.0.1:1080
2.在公司内网一台跳板机上建立通道,注意,需要可以上面那台访问外网服务器。
gost.exe -L=rtcp://127.0.0.1:9822/要控制的的服务器ip:22 -F=ss2://AEAD_CHACHA20_POLY1305:password@外网服务器ip:443?ping=300 -F socks5://127.0.0.1:1080
3.在外网服务器就可以访问内网的要控制的服务器了:
ssh -p 9822 root@127.0.0.1

Categories: 感悟 Tags:

国内网络十分不稳定,我们在用pip install或者docker拉取镜像的时候经常出现超时或者无法访问的情况,这时候可以找一台国外的云服务器,做个代理加速我们的安装进程。步骤如下:

国外云服务器1.20.20.31 ssh端口22

首先通过ssh建立一个socket代理,端口号是1080:

ssh -fND 127.0.0.1:1080 root@1.20.20.31 -p 22

但是我们设置环境变量的时候,没法设置socket代理,只能设置http和https的,所以我们需要借助一个工具gost来把socket代理转化为http的。

安装gost的方法:
------

#### 二进制文件

https://github.com/ginuerzh/gost/releases

#### 源码编译

```bash
go get -u github.com/ginuerzh/gost/cmd/gost
```

#### Docker

```bash
docker pull ginuerzh/gost
```

#### Ubuntu商店

```bash
sudo snap install gost

“`

运行如下命令启动一个1081的http代理:

gost -L :1081 -F socks5://127.0.0.1:1080

最后设置pip或者docker代理就可以了:

①永久设置:

vim /etc/profile:
export http_proxy='http://127.0.0.1:1081'
export https_proxy='http://127.0.0.1:1081'
source /etc/profile

②临时设置(重连后失效):
也可以直接运行

export http_proxy='http://127.0.0.1:1081'
export https_proxy='http://127.0.0.1:1081'

Categories: 分享 Tags:

基于以太坊私有链搭建一个分布式应用【3】0+

37,348 views / 2018.03.15 / 11:11 下午

搭建Dapp(分布式应用)

非常不情愿的安装npm,然后sudo npm install -g truffle

会出现一个很NB的动画安装过程,默默地欣赏…

71j.cn@ubuntu:~$ mkdir dapp

71j.cn@ubuntu:~$ cd dapp

truffle unbox webpack # 初始化一个带ui的truffle项目

/usr/bin/env: ‘node’: No such file or directory

sudo ln -s /usr/bin/nodejs /usr/bin/node

又报错:

SyntaxError: Block-scoped declarations (let, const, function, class) not yet supported outside strict mode

因为 node的版本太低

sudo npm install -g n

sudo n stable

再来truffle unbox webpack,成功后可以看下当前的目录结构:

-contracts //智能合约存放目录

-migrations //智能合约abi及部署信息存目录

-test //测试文件

-truffle.js //truffle默认配置

-truffle-config.js //Windows下默认配置文件名与truffle冲突,可使用该文件解决

修改webpack.config.js,添加如下代码制定运行的服务器ip,注意如果只监听localhost, 理论上无法通过IP:PORT的方式访问。

devServer: {

host: ‘10.120.113.245’,

port: 8080,

},

配置truffle.js到私链:

module.exports = {

// See <http://truffleframework.com/docs/advanced/configuration>

// to customize your Truffle configuration!

networks: {

development: {

host: “10.120.113.245”,

port: 8101,

network_id: “*”, // * Match any network id

gas:4712388, //Gas limit used for deploys. Default is web3.eth.getBlock(“pending”).gasLimit==4712388.

gasPrice:18000000000, //Gas price used for deploys. Default is 100000000000 (100 Shannon).

from: “0xf4182f0dc92313f8e106fc033641c3351703911f”//From address used during migrations. Defaults to the first available account provided by your Ethereum client.

// provider – web3 provider instance Truffle should use to talk to the Ethereum network.

//          – function that returns a web3 provider instance (see below.)

//          – if specified, host and port are ignored.

}

},

solc: {

optimizer: {

enabled: true,

runs: 200

}

},

};

 

truffle migrate # Build, compile, & deploy the dap

这个过程比较慢,耐心等待。

npm run dev

就可以通过浏览器http:// 10.120.113.245:8101访问了。

 

部署自己的合约程序

 

deploy//这都是要自己编写的,具体的看代码吧,点这里下载

├── app

│   ├── index.html

│   ├── javascripts

│   │   └── app.js

│   └── stylesheets

│       └── app.css

├── contracts

│   ├── Conference.sol

│   └── Migrations.sol//这个是truffle管理合约用的合约

└── test

└── conference.js

 

rm -rf dapp/app

rm -rf dapp/contracts

rm -rf dapp/test

然后把deploy中的文件都复制到dapp中,进入dapp:

71j.cn@ubuntu:~/dapp$ truffle migrate –reset

Using network ‘development’.

 

Running migration: 1_initial_migration.js

Replacing Migrations…

… 0x08a2db9ceda488eb534f4e0364dc21d569ad7a7d822c12f2007021ff67d8dc3b

Migrations: 0xec2264304e81fe2a3d5f3f316ecd627ea44c285d

Saving successful migration to network…

… 0xfa324a40aac5d0d89da7c05ddb5382bc2c56bd21e92a4f2b5b34325c526c7d5e

Saving artifacts…

Running migration: 2_deploy_contracts.js

Replacing Conference…

… 0x2c45e7be5c5a40770a99c4a59e1f09bdc69168c96d45be62e46c6ba053d77cda

Conference: 0x7d2c18809639c3a019e3b82c3ec39da526a8beb2

Saving successful migration to network…

… 0x37fe62d2822977d15a2261c28f599df0418e567b0a0ed38870e4d556d5f12d4c

Saving artifacts…

 

然后看看我们的测试用例编写的怎样?

appadmin@ubuntu:~/dapp$ truffle test

Using network ‘development’.

 

[ ‘0xf4182f0dc92313f8e106fc033641c3351703911f’,

‘0x899a969874bec249e4ef439c75adb0c358913b95’,

‘0x6797c39109ec4676d3b5dbb4dc76f1143a0f633a’ ]

 

 

Contract: Conference

✓ Initial conference settings should match (2068ms)

✓ Should update quota (3570ms)

1) Should let you buy a ticket

 

Events emitted during test:

—————————

 

Deposit(_from: 0x899a969874bec249e4ef439c75adb0c358913b95, _amount: 1000000000000000000)

 

—————————

✓ Should issue a refund by owner only (10310ms)

 

 

3 passing (20s)

1 failing

 

1) Contract: Conference Should let you buy a ticket:

AssertionError: Difference should be what was sent: expected 11001296558000000000 to equal ‘1000000000000000000’

at test/conference.js:63:11

at <anonymous>

at process._tickCallback (internal/process/next_tick.js:118:7)

 

为什么有一个没通过?因为那哥们在挖矿呢…,把挖矿的改成account[3]就好了。

npm run dev

通过浏览器访问http:// 10.120.113.245:8101即可。

Categories: 感悟 Tags:

基于以太坊私有链搭建一个分布式应用【2】1+

36,857 views / 2018.03.15 / 10:10 下午

 

合约编写

 

合约编辑器有很多,我们用这个IDE http://remix.ethereum.org/

合约格式是sol,需要编译成js文件让geth执行。以上工具可以方便的实现。

Conference.sol示例:

pragma solidity ^0.4.19;

 

contract Conference {  // can be killed, so the owner gets sent the money in the end

 

address public organizer;

mapping (address => uint) public registrantsPaid;

uint public numRegistrants;

uint256 public ticketPrice;

uint public quota;

 

event Deposit(address _from, uint _amount); // so you can log the event,more information to see http://71j.cn

event Refund(address _to, uint _amount); // so you can log the event,more information to see http://71j.cn

 

function Conference() public{

organizer = msg.sender;

quota = 100;

ticketPrice = 1 ether;

numRegistrants = 0;

}

/** 1 msg 指谁调用该智能合约发来的信息。 注意:智能合约也是一个账户

* 2 msg 包含的成员:

* msg.data(bytes): 获取调用者传入的数据信息

* msg.gas(uint): 剩余的以太币

* msg.sender(address): 当前调用者的以太坊账户地址

* msg.sig(bytes4): first four bytes of cakkData

* msg.value(uint): number of wei sent with the message;注意: 只有当有以太坊币的转义时, 该变量才有值(除调用合约所花费以太币这种情况)

*/

function buyTicket(uint ticketNumber) payable public {

require(ticketNumber>0);

require(numRegistrants+ticketNumber <= quota);

uint totalPrice = ticketNumber*ticketPrice;

require(msg.value==totalPrice);

registrantsPaid[msg.sender] += totalPrice;

numRegistrants+=ticketNumber;

//this.transfer(totalPrice);

organizer.transfer(totalPrice);

Deposit(msg.sender, totalPrice);

}

/*Contracts that receive Ether directly (without a function call, i.e. using send or transfer) but do not define a fallback function throw an exception, sending back the Ether (this was different before Solidity v0.4.0). So if you want your contract to receive Ether, you have to implement a fallback function.

*/

function () public payable {

/*只要有支付行为,就会调用这个后备

函数。举例:

*合约计划仅接受用户转入指定价值的eth,如果用户转入eth较少,则直接返回异常,当用户转入eth较多,则将用户多转入的eth退回。

*在这里我们可以借助 fallback function,也就是后备函数进行转入金额的限定。

*/

 

}

 

function changeQuota(uint newquota) public {

require(msg.sender == organizer);

quota = newquota;

}

 

function refundTicket(address recipient, uint amount,uint ticketNumber) payable public{

require(msg.sender == organizer);

if (registrantsPaid[recipient] >= amount) {

//address myAddress = this;

if (organizer.balance >= amount) {

recipient.transfer(amount);

Refund(recipient, amount);

registrantsPaid[recipient] -= amount;

numRegistrants-=ticketNumber;

}

}

return;

}

 

 

function destroy () public{

require(msg.sender == organizer);// without this funds could be locked in the contract forever!

selfdestruct(organizer);

 

}

}

*在Node1命令行中审视一下自己编辑的合约:solc –gas Conference.sol

*网上说以太坊在标准库方面只实现了非常非常少的一部分,比如SHA3.SHA3操作的基本开销是30gas,之后每个32字节的数据hash需要3gas。内置的StringEqual操作需要5 gas的基本开销,随后比较每个字节需要1 gas

 

用IDE编译成Conference,js后,回到geth,可以导入合约。!注意,这个过程矿机不能停。!

 

> loadScript(‘./Conference.js’)

null [object Object]

true

> null [object Object]

Contract mined! address: 0xcbf1b22ad7eda3482f7f00948539ea5684a6882b transactionHash: 0xb7c3a8c1282baef3c7b7063b1a96b10e28ff26a2c1fe9fd5a3c81ccbafcbea7d

 

我们可以看到合约有一个address,意味着它能收发以太值,以及一个transactionHash,用它来找到区块链中的位置。直接输入ss(我们初始化的实例名字),发现合约的abi信息以及公共的可调用的函数也列出了。abi是Application Binary Interface的缩写,字面意思 应用二进制接口,可以通俗的理解为合约的接口说明。当合约被编译后,那么它的abi也就确定

现在让我们调用下:

ss.changeQuota.sendTransaction(100)

Error: invalid address

at web3.js:3930:15

at web3.js:3756:20

at web3.js:5025:28

at map (<native code>)

at web3.js:5024:12

at web3.js:5050:18

at web3.js:5075:23

at web3.js:4137:16

at <anonymous>:1:1

我去,没成功。再试试这个:

> ss.quota()

100

> ss.changeQuota.sendTransaction(200,{from:eth.coinbase})

“0x44acef0216b4da50e32a25bdaaf074ddd349f23280f181bf0d284c32769830d2”

> ss.quota()

100

> ss.quota()

200

由于web3.js封装了合约调用的方法。我们可以使用可以使用web3.eth.contract的里的sendTransaction来修改区块链数据。在这里有个坑,有可能会出现Error: invalid address,原因是没有传from,交易发起者的地址。在使用web3.js的API都需留意,出现这种找不到地址的,都看看from字段吧。

 

接下来看看gas消耗的情况:

Node1:

> ss.organizer()

“0xf4182f0dc92313f8e106fc033641c3351703911f”

> eth.getBalance(“0xf4182f0dc92313f8e106fc033641c3351703911f”)

2.0000037903157114008e+28

为了保证合约内容讲解的完成性,这块我们要穿越到第二天Node2节点已经搭建完成了!

Node2:

var bb=eth.contract.(abi).at(address);//这是在新节点初始化合约的方法

> personal.unlockAccount(eth.coinbase)

Unlock account 0xf2ad598bb92b28989d1e8bf686f0e07a8a036a5a

Passphrase:

true

> eth.getBalance(eth.coinbase)

9.574379388346e+21

> bb.buyTicket.sendTransaction(2,{from:eth.coinbase,value:web3.toWei(2)})

“0x2f5bf547eadbff0c96db240aa896990142bd23f769be87f2e459261caf8d633c”

> eth.getBalance(eth.coinbase)

9.572378091716e+21

> eth.getTransaction(“0x2f5bf547eadbff0c96db240aa896990142bd23f769be87f2e459261caf8d633c”)

{

blockHash: “0xb207210430239571b5d999c4c000f065d66e660b64045e6218e179a817f8c006”,

blockNumber: 8422,

from: “0xf2ad598bb92b28989d1e8bf686f0e07a8a036a5a”,

gas: 90000,

gasPrice: 18000000000,

hash: “0x2f5bf547eadbff0c96db240aa896990142bd23f769be87f2e459261caf8d633c”,

input: “0x67dd74ca0000000000000000000000000000000000000000000000000000000000000002”,

nonce: 42,

r: “0xef743cab7bdf70706052877ab675a289094a56e1f0241f618b7eb7ad73b273a7”,

s: “0x5cb18b44f1e3276c8f695ad126a578ebe1badd2f532b7d4139592eb2bd3e05f7”,

to: “0xfbcd7a73ff412512117300f59248f4281579c80f”,

transactionIndex: 0,

v: “0x3c”,

value: 2000000000000000000

}

>  9.574379388346e+21-9.572378091716e+21//少了这么多钱

2001296630000648200

> 2001296630000648200-2000000000000000000//减掉票钱

1296630000648192

> 1296630000648192/18000000000

72035.00003601066//大概花了这么多gas

 

创建新Node

登录246服务器,修改genesis.json!!!!!错误!!!!这文件一个字都不能改。

geth –datadir “/home/appadmin/.ethereum” init eth/genesis.json

./start.sh删除unlock和password, 修改rpcaddr为246

 

 

在Node1的 boot node 上获取 enode 信息

> admin.nodeInfo.enode

“enode://2794bb6a9439d80c55c394328b396105adff8f5a2c9686fd56945024ebadb847ac2bbca2266742138ddfb94a03ceccbcd98b1caffc4820d1ead5c369234aad8b@[::]:30303”

替换:

“enode://2794bb6a9439d80c55c394328b396105adff8f5a2c9686fd56945024ebadb847ac2bbca2266742138ddfb94a03ceccbcd98b1caffc4820d1ead5c369234aad8b@10.120.113.245:30303”

将 boot node 的 enode 信息写入 node2

>admin.addPeer(“enode://2794bb6a9439d80c55c394328b396105adff8f5a2c9686fd56945024ebadb847ac2bbca2266742138ddfb94a03ceccbcd98b1caffc4820d1ead5c369234aad8b@10.120.113.245:30303”)

true

> admin.peers

[]

> admin.peers

[{

caps: [“eth/62”, “eth/63”],

id: “edcc39c963e128555c192be58ddd387a674aba2123ec7daef9144b6ff109ac5712bcfea3904a63031fcdbbfae30c26bb6148af0c559944b34f00d0cd2889688c”,

name: “Geth/v1.8.2-stable-b8b9f7f4/linux-amd64/go1.9.4”,

network: {

inbound: true,

localAddress: “10.120.113.245:30303”,

remoteAddress: “10.120.113.246:48204”,

static: false,

trusted: false

},

protocols: {

eth: {
difficulty: 7035219,

head: “0xf8b772c1b23cd8bea305aa52f13cd68a1697339e86f9badd5d064502c3c84e65”,

version: 63

}

}

}]

也可以在start.sh中直接指定Node1的信息

#!/bin/sh

geth –ipcdisable –rpc –rpcaddr “10.120.113.246”  –port 30303 –rpcport 8101 –rpccorsdomain “*” –networkid 12 –datadir ‘~/.ethereum’ –bootnodes “enode://3d6ff07e12e59a4dbd972e5d8ed33578570203c852f9837b263a04fa91bc9c795b46cbd3ee801a67aa741e317fcd734409bf1e81c8a4e81e6e27f420fd25ba0a@10.120.113.245:30303” console 2>>log

这样新节点就添加完成了,他们之间的通信就是p2p的方式了。

Categories: 感悟 Tags: