所以我有一个事件循环,这将是run_until_complete我的accept_connection方法
@asyncio.coroutine
def accept_connection(self):
assert self.server_socket is not None
while True:
client, addr = yield from self.loop.sock_accept(self.server_socket)
asyncio.async(self.handle_connection(client, addr))
Run Code Online (Sandbox Code Playgroud)
我的handle_connection方法看起来像这样
def handle_connection(self, client, addr):
#removed error checking
while True:
try:
yield from asyncio.wait([con.handle_read_from_connection()], timeout=5.0)
except (AssertionError, PacketException):
print("Invalid packet detected!")
Run Code Online (Sandbox Code Playgroud)
最后我的handle_read_from_connection(目前)看起来像这样:
@asyncio.coroutine
def handle_read_from_connection(self):
raise PacketException("hello")
Run Code Online (Sandbox Code Playgroud)
因此,此方法应始终引发错误并按下try catch语句的except块并打印检测到的无效数据包.相反,我得到的是追溯!
future: Task(<handle_read_from_connection>)<exception=PacketException('hello',)>
Traceback (most recent call last):
File "/usr/lib/python3.4/asyncio/tasks.py", line 283, in _step
result = next(coro)
File "/some_path.py", line 29, in handle_read_from_connection
raise PacketException("hello") …Run Code Online (Sandbox Code Playgroud) 所以我向带有pythons请求库的服务器发出了请求.代码看起来像这样(它使用适配器,因此它需要匹配某个模式)
def getRequest(self, url, header):
"""
implementation of a get request
"""
conn = requests.get(url, headers=header)
newBody = conn.content
newHeader = conn.headers
newHeader['status'] = conn.status_code
response = {"Headers" : newHeader, "Body" : newBody.decode('utf-8')}
self._huddleErrors.handleResponseError(response)
return response
Run Code Online (Sandbox Code Playgroud)
我正在解析的头参数是这个
{'Authorization': 'OAuth2 handsOffMyToken', 'Accept': 'application/vnd.huddle.data+json'}
Run Code Online (Sandbox Code Playgroud)
但是我从服务器返回xml响应.检查fiddler后,我看到发送的请求是:
Accept-Encoding: identity
Accept: */*
Host: api.huddle.dev
Authorization: OAuth2 HandsOffMyToken
Accept: application/vnd.huddle.data+json
Accept-Encoding: gzip, deflate, compress
User-Agent: python-requests/1.2.3 CPython/3.3.2 Windows/2008ServerR2
Run Code Online (Sandbox Code Playgroud)
我们可以看到有2个接受标题!请求库正在添加此Accept:*/*标头,这将丢弃服务器.有谁知道我怎么能阻止这个?
我有一个看起来像这样的功能
this.getToken = function() {
if (token === null) {
token = getAccessTokenAsync("username", "password");
lastTokenTime = getTokenExpiryAsync();
}
}
Run Code Online (Sandbox Code Playgroud)
此函数将调用getAccessTokenAsync,它将使用xhr向我的Web服务器发出请求.这看起来像这样:
getAccessTokenAsync = function (username, password) {
var serializedData = {
username: username, password: password,
};
return new WinJS.Promise(function (complete) {
WinJS.xhr({
type: "post",
url: "http://127.0.0.1:8080/authenticate/login",
responseType: "json",
data: JSON.stringify(serializedData)
}).done(
function complete(result){
return JSON.parse(result.responseText);
}
);
})
}
Run Code Online (Sandbox Code Playgroud)
我希望令牌现在可以存储一个承诺.当我们调用.done()或.next()将拥有服务器返回的json对象时.但是,当我打电话给getTokenExpiryAsync()别的事情发生.
getTokenExpiryAsync = function () {
if (token === null) {
return new Date();
}
token.then( …Run Code Online (Sandbox Code Playgroud) UPDATE
从gob编码转到json修复了这个问题.但是,我仍然想知道为什么这与gob无法合作.
所以我的客户端代码看起来像这样
account := new(database.Account)
err := client.Call("AccountDb.FindAccount", "username", account)
if err != nil {
logger.FATAL.Print(err.Error())
return
}
logger.INFO.Print(account)
Run Code Online (Sandbox Code Playgroud)
在服务器端AccountDb.FindAccount看起来像这样
func (t *AccountDb) FindAccount(args *string, reply *Account) error {
reply.Username = "this is a test"
return nil
}
Run Code Online (Sandbox Code Playgroud)
Account的结构看起来像这样
type Account struct {
Id int
Username string
Password string
Email string
Created time.Time
LastLoggedIn time.Time
AccessLevel int
Banned struct {
reason string
expires time.Time
}
}
Run Code Online (Sandbox Code Playgroud)
如果我尝试执行rpc请求开始,服务器执行该过程.然而程序然后挂起,程序不会返回!但是,如果我从Account结构中删除禁止的匿名结构,它工作正常!为什么是这样?有这个问题的解决方案吗?
编辑客户端和服务器注册码如下所示
客户
client, err = rpc.DialHTTP("tcp", "127.0.0.1:9001")
if err != …Run Code Online (Sandbox Code Playgroud) 有没有办法让我可以将一个类扩展cmdlet写入powershell控制台而不将其写入管道?
我正在编写一个cmdlet,它会将大量对象写入管道(然后人们将管道输入另一个cmdlet,如Export-CSV).问题是,如果我开始将信息写入控制台,也会被提起.
例如,对象1对象2"我是通过this.WriteObjct对象3记录到powershell的信息消息
如果我尝试将cmdlet管道传输到另一个cmdlet中,我将获得对象1,2,信息消息和3.我需要它,因此它只管道对象1,2和3.
谁能指出我正确的方向?
所以我有一个名为self.outliers的字典,看起来像这样
{
"foo" : {"freq" : 7, "ids" : [1,2,3,4,5,6,7]},
"bar" : {"freq" : 2, "ids" : [8,9]}
}
Run Code Online (Sandbox Code Playgroud)
我正在尝试提取频率>然后为2的所有键值我尝试过:
{(k, self.outliers.get(k)) for k in self.outliers if self.outliers[k]['freq'] > 1}
Run Code Online (Sandbox Code Playgroud)
任何人都可以帮我解决这个问题,因为我一直在反对这个问题.
python ×3
c# ×1
cmdlet ×1
go ×1
http-headers ×1
javascript ×1
powershell ×1
python-3.3 ×1
rpc ×1
try-catch ×1
winjs ×1