gpt4 book ai didi

http - F# 网络服务器库

转载 作者:可可西里 更新时间:2023-11-01 15:11:15 26 4
gpt4 key购买 nike

是否有用于 F# 的 Web 服务器库,类似于 Python 中的 SimpleHTTPServer?

安装像 IIS 这样的完整服务器对我想要的东西来说有点过分了,它是一个简单的应用程序,可以使用 Web 浏览器进行查询,有效地使用 HTTP 作为监视方法。理想情况下,对地址 /engines/id/state 的请求将映射到我提供的函数 get_state(engine_id)

最佳答案

自托管 WCF 服务是一个不错的开始;这是初学者的一个小例子:

open System
open System.IO
// add reference to these two guys, need .NET full (not client profile)
open System.ServiceModel
open System.ServiceModel.Web

[<ServiceContract>]
type MyContract() =
[<OperationContract>]
[<WebGet(UriTemplate="{s}/{t}")>]
member this.Get(s:string, t:string) : Stream =
let html = sprintf @"
<!DOCTYPE HTML PUBLIC ""-//W3C//DTD HTML 4.0 Transitional//EN"">
<html><head></head><body>Called with '%s' and '%s'</body></html>" s t
upcast new MemoryStream(System.Text.Encoding.UTF8.GetBytes(html))

let Main() =
let address = "http://localhost:64385/"
let host = new WebServiceHost(typeof<MyContract>, new Uri(address))
host.AddServiceEndpoint(typeof<MyContract>, new WebHttpBinding(), "")
|> ignore
host.Open()
printfn "Server running at %s" address
printfn "Press a key to close server"
System.Console.ReadKey() |> ignore
host.Close()

Main()
// now go hit
// http://localhost:64385/foo/42
// in your browser

关于http - F# 网络服务器库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3773812/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com