到目前为止我在网络api上使用Microsoft WebSockets的所有示例都使用IIS,实现是在get方法上将HTTP连接升级为websocket,并将websocket处理程序的实例传递给HTTPContext
public HttpResponseMessage Get() {
if (HttpContext.Current.IsWebSocketRequest) {
var noteHandler = new NoteSocketHandler();
HttpContext.Current.AcceptWebSocketRequest(noteHandler);
}
return new HttpResponseMessage(HttpStatusCode.SwitchingProtocols);
}
Run Code Online (Sandbox Code Playgroud)
我想要实现的是在OWIN管道上做同样的事情.我面临的问题是连接正在升级以使用Websockets,但它没有使用我的websocket处理程序.我哪里错了?请建议.
使用OwinContext的控制器(遵循Nancy使用OWIN的示例WebSockets),
public HttpResponseMessage Get() {
IOwinContext owinContext = Request.GetOwinContext();
WebSocketAccept acceptToken = owinContext.Get<WebSocketAccept>("websocket.Accept");
if (acceptToken != null) {
var requestHeaders = GetValue<IDictionary<string, string[]>>(owinContext.Environment, "owin.RequestHeaders");
Dictionary<string, object> acceptOptions = null;
string[] subProtocols;
if (requestHeaders.TryGetValue("Sec-WebSocket-Protocol", out subProtocols) && subProtocols.Length > 0) {
acceptOptions = new Dictionary<string, object>();
// Select the first one from the client
acceptOptions.Add("websocket.SubProtocol", subProtocols[0].Split(',').First().Trim()); …Run Code Online (Sandbox Code Playgroud) import os
import sys
files = os.listdir(sys.argv[1])
for file in files:
if file[-4:] == ".png":
os.rename(file, file.replace('\r', ''))
Run Code Online (Sandbox Code Playgroud)
我使用上面的代码从文件名中删除\ r \n,但有些如何执行时我得到以下错误
Traceback (most recent call last):
File "renameImages.py", line 9, in <module>
os.rename(f, f.replace('\r', ''))
OSError: [Errno 2] No such file or directory
Run Code Online (Sandbox Code Playgroud)
我哪里错了?
我通过调用构造函数中的函数运行下面的代码
第一 -
>>> class PrintName:
... def __init__(self, value):
... self._value = value
... printName(self._value)
... def printName(self, value):
... for c in value:
... print c
...
>>> o = PrintName('Chaitanya')
C
h
a
i
t
a
n
y
a
Run Code Online (Sandbox Code Playgroud)
我再次运行这个,我得到了这个
>>> class PrintName:
... def __init__(self, value):
... self._value = value
... printName(self._value)
... def printName(self, value):
... for c in value:
... print c
...
>>> o = PrintName('Hello')
Traceback (most recent call last):
File "<stdin>", …Run Code Online (Sandbox Code Playgroud)