Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
H
http-server
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
1
Merge Requests
1
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
cython-plus
http-server
Commits
55152cf7
Commit
55152cf7
authored
Nov 01, 2021
by
Xavier Thompson
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add server.pyx and client.pyx demo
parent
fbe547e6
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
95 additions
and
0 deletions
+95
-0
src/client.pyx
src/client.pyx
+41
-0
src/server.pyx
src/server.pyx
+47
-0
src/stdlib/_socket.pxd
src/stdlib/_socket.pxd
+7
-0
No files found.
src/client.pyx
0 → 100644
View file @
55152cf7
from
stdlib.string
cimport
Str
from
stdlib.socket
cimport
getaddrinfo
,
socket
,
AF_UNSPEC
,
SOCK_STREAM
,
AI_PASSIVE
,
MSG_WAITALL
,
SHUT_WR
,
SHUT_RD
from
libc.stdio
cimport
puts
,
printf
def
main
():
cdef
const
char
*
arg
while
True
:
b
=
input
(
"Message to send:
\
n
"
).
encode
()
arg
=
b
with
nogil
:
a
=
getaddrinfo
(
NULL
,
Str
(
"3490"
),
AF_UNSPEC
,
SOCK_STREAM
,
0
,
AI_PASSIVE
)[
0
]
s
=
socket
(
a
.
family
,
a
.
socktype
,
a
.
protocol
)
puts
(
'open: ok'
)
s
.
connect
(
a
.
sockaddr
)
puts
(
'connect: ok'
)
msg
=
Str
(
arg
)
s
.
sendall
(
msg
)
puts
(
'send: ok'
)
s
.
shutdown
(
SHUT_WR
)
r
=
Str
(
''
)
while
True
:
rcv
=
s
.
recv
(
100
)
if
rcv
.
__len__
()
>
0
:
printf
(
'recv: %s
\
n
'
,
Str
.
to_c_str
(
rcv
))
r
=
r
+
rcv
else
:
puts
(
'connection closed by server'
)
break
printf
(
'got: %s
\
n
'
,
Str
.
to_c_str
(
r
))
s
.
close
()
puts
(
'close: ok'
)
main
()
src/server.pyx
0 → 100644
View file @
55152cf7
from
stdlib.string
cimport
Str
from
stdlib.socket
cimport
getaddrinfo
,
socket
,
AF_UNSPEC
,
SOCK_STREAM
,
AI_PASSIVE
,
SHUT_RD
,
SHUT_WR
from
libc.stdio
cimport
puts
,
printf
def
main
():
with
nogil
:
a
=
getaddrinfo
(
NULL
,
Str
(
"3490"
),
AF_UNSPEC
,
SOCK_STREAM
,
0
,
AI_PASSIVE
)[
0
]
s
=
socket
(
a
.
family
,
a
.
socktype
,
a
.
protocol
)
puts
(
'open: ok'
)
s
.
bind
(
a
.
sockaddr
)
puts
(
'bind: ok'
)
s
.
listen
(
5
)
puts
(
'listen: ok'
)
while
True
:
s1
=
s
.
accept
()
puts
(
'accept: ok'
)
r
=
Str
(
''
)
while
True
:
rcv
=
s1
.
recv
(
100
)
if
rcv
.
__len__
()
>
0
:
printf
(
'recv: %s
\
n
'
,
Str
.
to_c_str
(
rcv
))
r
=
r
+
rcv
else
:
puts
(
'connection closed by client'
)
break
s1
.
shutdown
(
SHUT_RD
)
s1
.
sendall
(
r
)
puts
(
'send: ok'
)
s1
.
shutdown
(
SHUT_WR
)
s1
.
close
()
puts
(
'close 1: ok'
)
s
.
close
()
puts
(
'close 0: ok'
)
main
()
src/stdlib/_socket.pxd
View file @
55152cf7
...
...
@@ -105,6 +105,13 @@ cdef extern from "<netinet/in.h>" nogil:
enum
:
INET6_ADDRSTRLEN
unsigned
long
htonl
(
unsigned
long
hostlong
)
unsigned
short
htons
(
unsigned
short
hostshort
)
unsigned
long
ntohl
(
unsigned
long
netlong
)
unsigned
short
ntohs
(
unsigned
short
netshort
)
cdef
extern
from
"<arpa/inet.h>"
nogil
:
const
char
*
inet_ntop
(
int
af
,
const
void
*
src
,
char
*
dst
,
socklen_t
size
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment