Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit8087ccf

Browse files
committed
Add socket.getsockname
1 parentdc0bd73 commit8087ccf

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

‎tests/snippets/stdlib_socket.py‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
importsocket
22
fromtestutilsimportassertRaises
33

4+
45
listener=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
5-
listener.bind(("127.0.0.1",8080))
6+
listener.bind(("127.0.0.1",0))
67
listener.listen(1)
78

89
connector=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
9-
connector.connect(("127.0.0.1",8080))
10+
connector.connect(("127.0.0.1",listener.getsockname()[1]))
1011
connection=listener.accept()[0]
1112

1213
message_a=b'aaaa'

‎vm/src/stdlib/socket.rs‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,13 @@ impl Connection {
6262
_ =>Err(io::Error::new(io::ErrorKind::Other,"oh no!")),
6363
}
6464
}
65+
66+
fnlocal_addr(&self) -> io::Result<SocketAddr>{
67+
matchself{
68+
Connection::TcpListener(con) => con.local_addr(),
69+
_ =>Err(io::Error::new(io::ErrorKind::Other,"oh no!")),
70+
}
71+
}
6572
}
6673

6774
implReadforConnection{
@@ -293,6 +300,33 @@ fn socket_close(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
293300
}
294301
}
295302

303+
fnsocket_getsockname(vm:&mutVirtualMachine,args:PyFuncArgs) ->PyResult{
304+
arg_check!(vm, args, required =[(zelf,None)]);
305+
match zelf.payload{
306+
PyObjectPayload::Socket{ref socket} =>{
307+
let addr =match socket.borrow_mut().con{
308+
Some(refmut v) => v.local_addr(),
309+
None =>returnErr(vm.new_type_error("".to_string())),
310+
};
311+
312+
match addr{
313+
Ok(addr) =>{
314+
let port = vm.ctx.new_int(addr.port());
315+
let ip = vm.ctx.new_str(addr.ip().to_string());
316+
let elements =RefCell::new(vec![ip, port]);
317+
318+
Ok(PyObject::new(
319+
PyObjectPayload::Sequence{ elements},
320+
vm.ctx.tuple_type(),
321+
))
322+
}
323+
_ =>returnErr(vm.new_type_error("".to_string())),
324+
}
325+
}
326+
_ =>Err(vm.new_type_error("".to_string())),
327+
}
328+
}
329+
296330
pubfnmk_module(ctx:&PyContext) ->PyObjectRef{
297331
let py_mod = ctx.new_module(&"socket".to_string(), ctx.new_scope(None));
298332

@@ -318,6 +352,7 @@ pub fn mk_module(ctx: &PyContext) -> PyObjectRef {
318352
ctx.set_attr(&socket,"accept", ctx.new_rustfunc(socket_accept));
319353
ctx.set_attr(&socket,"listen", ctx.new_rustfunc(socket_listen));
320354
ctx.set_attr(&socket,"close", ctx.new_rustfunc(socket_close));
355+
ctx.set_attr(&socket,"getsockname", ctx.new_rustfunc(socket_getsockname));
321356
socket
322357
};
323358
ctx.set_attr(&py_mod,"socket", socket.clone());

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp