FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
vscode-python/python_files/testing_tools/socket_manager.py at main · levrik/vscode-python · GitHub
levrik
/
vscode-python
Public
forked from
microsoft/vscode-python
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
vscode-python
/
python_files
/
testing_tools
/
socket_manager.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
118 lines (96 loc) · 3.41 KB
Breadcrumbs
vscode-python
/
python_files
/
testing_tools
/
socket_manager.py
Copy path
File metadata and controls
118 lines (96 loc) · 3.41 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.
import
socket
import
sys
# set the socket before it gets blocked or overwritten by a user tests
_SOCKET
=
socket
.
socket
class
PipeManager
:
def
__init__
(
self
,
name
):
self
.
name
=
name
def
__enter__
(
self
):
return
self
.
connect
()
def
__exit__
(
self
,
*
_
):
self
.
close
()
def
connect
(
self
):
if
sys
.
platform
==
"win32"
:
self
.
_writer
=
open
(
self
.
name
,
"wt"
,
encoding
=
"utf-8"
)
# reader created in read method
else
:
self
.
_socket
=
_SOCKET
(
socket
.
AF_UNIX
,
socket
.
SOCK_STREAM
)
self
.
_socket
.
connect
(
self
.
name
)
return
self
def
close
(
self
):
if
sys
.
platform
==
"win32"
:
self
.
_writer
.
close
()
else
:
# add exception catch
self
.
_socket
.
close
()
def
write
(
self
,
data
:
str
):
if
sys
.
platform
==
"win32"
:
try
:
# for windows, is should only use \n\n
request
=
(
f"""content-length:
{
len
(
data
)
}
\n
content-type: application/json
\n
\n
{
data
}
"""
)
self
.
_writer
.
write
(
request
)
self
.
_writer
.
flush
()
except
Exception
as
e
:
print
(
"error attempting to write to pipe"
,
e
)
raise
(
e
)
else
:
# must include the carriage-return defined (as \r\n) for unix systems
request
=
(
f"""content-length:
{
len
(
data
)
}
\r
\n
content-type: application/json
\r
\n
\r
\n
{
data
}
"""
)
self
.
_socket
.
send
(
request
.
encode
(
"utf-8"
))
def
read
(
self
,
bufsize
=
1024
)
->
str
:
"""Read data from the socket.
Args:
bufsize (int): Number of bytes to read from the socket.
Returns:
data (str): Data received from the socket.
"""
if
sys
.
platform
==
"win32"
:
# returns a string automatically from read
if
not
hasattr
(
self
,
"_reader"
):
self
.
_reader
=
open
(
self
.
name
,
"rt"
,
encoding
=
"utf-8"
)
return
self
.
_reader
.
read
(
bufsize
)
else
:
# receive bytes and convert to string
while
True
:
part
:
bytes
=
self
.
_socket
.
recv
(
bufsize
)
data
:
str
=
part
.
decode
(
"utf-8"
)
return
data
class
SocketManager
(
object
):
"""Create a socket and connect to the given address.
The address is a (host: str, port: int) tuple.
Example usage:
```
with SocketManager(("localhost", 6767)) as sock:
request = json.dumps(payload)
result = s.socket.sendall(request.encode("utf-8"))
```
"""
def
__init__
(
self
,
addr
):
self
.
addr
=
addr
self
.
socket
=
None
def
__enter__
(
self
):
return
self
.
connect
()
def
__exit__
(
self
,
*
_
):
self
.
close
()
def
connect
(
self
):
self
.
socket
=
socket
.
socket
(
socket
.
AF_INET
,
socket
.
SOCK_STREAM
,
socket
.
IPPROTO_TCP
)
if
sys
.
platform
==
"win32"
:
addr_use
=
socket
.
SO_EXCLUSIVEADDRUSE
else
:
addr_use
=
socket
.
SO_REUSEADDR
self
.
socket
.
setsockopt
(
socket
.
SOL_SOCKET
,
addr_use
,
1
)
self
.
socket
.
connect
(
self
.
addr
)
return
self
def
close
(
self
):
if
self
.
socket
:
try
:
self
.
socket
.
shutdown
(
socket
.
SHUT_RDWR
)
except
Exception
:
pass
self
.
socket
.
close
()
Back
|
FazBrowse Home
|
New Git URL