Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ A brief description for `example`
| example/fibonacci.py | Fibonacci, which contains loop and recursion |
| example/fibonacci_env.py | Call python/native function in wasm |
| example/pi.py | Calculate π using the agm algorithm |
| example/wasi_ll.py | Execute a wasi program and list information about a directory |
| example/wasi_httpbin.py | Execute a wasi program and http get from <http://httpbin.org> |
| example/wasi_ll.py | Execute a wasi program and list information about a directory |
| example/wasi_zen.py | Execute a wasi program and print poem "The Zen of Python" |

# Test
Expand Down
31 changes: 31 additions & 0 deletions example/wasi_httpbin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import pywasm
import socket

runtime = pywasm.core.Runtime()

client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host = 'httpbin.org'
port = 80
client_socket.connect((host, port))
rights = sum([
pywasm.wasi.Preview1.RIGHTS_FD_READ,
pywasm.wasi.Preview1.RIGHTS_FD_WRITE,
pywasm.wasi.Preview1.RIGHTS_SOCK_SHUTDOWN,
])

wasi = pywasm.wasi.Preview1(['wasi_httpbin.wasm'], {}, {})
wasi.fd.append(pywasm.wasi.Preview1.File(
host_fd=client_socket.fileno(),
host_name=client_socket.getsockname(),
host_status=pywasm.wasi.Preview1.FILE_STATUS_OPENED,
pipe=None,
wasm_fd=3,
wasm_flag=0,
wasm_name=client_socket.getsockname(),
wasm_rights_base=rights,
wasm_rights_root=rights,
wasm_status=pywasm.wasi.Preview1.FILE_STATUS_OPENED,
wasm_type=pywasm.wasi.Preview1.FILETYPE_SOCKET_STREAM,
))
wasi.bind(runtime)
wasi.main(runtime, runtime.instance_from_file('example/wasi_httpbin/bin/wasi_httpbin.wasm'))
2 changes: 2 additions & 0 deletions example/wasi_httpbin/.cargo/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[build]
target = "wasm32-wasip1"
1 change: 1 addition & 0 deletions example/wasi_httpbin/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/target
16 changes: 16 additions & 0 deletions example/wasi_httpbin/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions example/wasi_httpbin/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "wasi_httpbin"
version = "1.0.0"
edition = "2024"

[dependencies]
wasi = "0.11.0+wasi-snapshot-preview1"
Binary file added example/wasi_httpbin/bin/wasi_httpbin.wasm
Binary file not shown.
Loading
Loading