File tree 1 file changed +9
-4
lines changed
1 file changed +9
-4
lines changed Original file line number Diff line number Diff line change @@ -2,7 +2,7 @@ use pyo3::prelude::*;
2
2
3
3
use log:: debug;
4
4
use socket2:: { Domain , Protocol , Socket , Type } ;
5
- use std:: net:: SocketAddr ;
5
+ use std:: net:: { IpAddr , SocketAddr } ;
6
6
7
7
#[ pyclass]
8
8
#[ derive( Debug ) ]
@@ -13,9 +13,14 @@ pub struct SocketHeld {
13
13
#[ pymethods]
14
14
impl SocketHeld {
15
15
#[ new]
16
- pub fn new ( address : String , port : i32 ) -> PyResult < SocketHeld > {
17
- let socket = Socket :: new ( Domain :: IPV4 , Type :: STREAM , Some ( Protocol :: TCP ) ) ?;
18
- let address: SocketAddr = format ! ( "{}:{}" , address, port) . parse ( ) ?;
16
+ pub fn new ( ip : String , port : u16 ) -> PyResult < SocketHeld > {
17
+ let ip: IpAddr = ip. parse ( ) ?;
18
+ let socket = if ip. is_ipv4 ( ) {
19
+ Socket :: new ( Domain :: IPV4 , Type :: STREAM , Some ( Protocol :: TCP ) ) ?
20
+ } else {
21
+ Socket :: new ( Domain :: IPV6 , Type :: STREAM , Some ( Protocol :: TCP ) ) ?
22
+ } ;
23
+ let address = SocketAddr :: new ( ip, port) ;
19
24
debug ! ( "{}" , address) ;
20
25
// reuse port is not available on windows
21
26
#[ cfg( not( target_os = "windows" ) ) ]
You can’t perform that action at this time.
0 commit comments