Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
S
Sockets Workshop Apr 2021
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Issue analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Sibidharan
Sockets Workshop Apr 2021
Compare revisions
949758447616d87f5158d01a4b03c2e4cfbe99e4 to master
Compare revisions
Changes are shown as if the
source
revision was being merged into the
target
revision.
Learn more about comparing revisions.
Source
sibidharan/sockets-workshop-apr-2021
Select target project
No results found
master
Select Git revision
Branches
master
Swap
Target
jaisabarish/sockets-workshop-apr-2021
Select target project
sibidharan/sockets-workshop-apr-2021
monish-palanikumar/sockets-workshop-apr-2021
ganesha005/sockets-workshop-apr-2021
jaisabarish/sockets-workshop-apr-2021
rii/sockets-workshop-apr-2021
spugazh398/sockets-workshop-apr-2021
6 results
949758447616d87f5158d01a4b03c2e4cfbe99e4
Select Git revision
Branches
jaisabarish-master-patch-55554
master
Show changes
Only incoming changes from source
Include changes to target since source was created
Compare
Commits on Source (4)
Client code
· 51479346
Sibidharan
authored
3 years ago
51479346
Client code
· f73a98a4
Sibidharan
authored
3 years ago
f73a98a4
Client code
· 6c944691
Sibidharan
authored
3 years ago
6c944691
mqt code
· 8060e292
Sibidharan
authored
3 years ago
8060e292
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
basic.py
+34
-9
34 additions, 9 deletions
basic.py
client.py
+2
-2
2 additions, 2 deletions
client.py
log.txt
+0
-0
0 additions, 0 deletions
log.txt
mqtt-chat-npy.py
+64
-0
64 additions, 0 deletions
mqtt-chat-npy.py
mqtt_basics.py
+21
-0
21 additions, 0 deletions
mqtt_basics.py
with
121 additions
and
11 deletions
basic.py
View file @
8060e292
...
...
@@ -3,14 +3,42 @@
#Hostname: learn.selfmade.ninja
import
socket
from
threading
import
Thread
HOST
=
''
PORT
=
3074
def
start_chat_thread
(
conn
,
addr
):
t
=
ChatMsgThread
(
conn
,
addr
)
t
.
start
()
print
(
"
Thread created and offloaded connection with IP {}:{}
"
.
format
(
addr
[
0
],
addr
[
1
]))
class
ChatMsgThread
(
Thread
):
def
__init__
(
self
,
conn
,
addr
):
Thread
.
__init__
(
self
)
self
.
conn
=
conn
self
.
addr
=
addr
#print("IP {} connected".format(addr[0]))
def
run
(
self
):
self
.
conn
.
sendall
(
b
"
Send your message, I will be able to see it here.
\n
"
)
while
(
True
):
try
:
data
=
self
.
conn
.
recv
(
2048
)
if
not
data
:
self
.
conn
.
close
()
continue
else
:
try
:
data
=
data
.
decode
()
print
(
data
)
except
:
pass
except
:
pass
s
=
socket
.
socket
(
socket
.
AF_INET
,
socket
.
SOCK_STREAM
)
#AF - Address Family
#INET - Internet
s
.
setsockopt
(
socket
.
SOL_SOCKET
,
socket
.
SO_REUSEADDR
,
1
)
s
.
bind
((
HOST
,
PORT
))
s
.
listen
()
...
...
@@ -19,10 +47,7 @@ while(True):
#conn - the connection object with the client
#addr - array 0 - IP address of the client
# array 1 - The back port with which the connection is established
print
(
"
IP {} connected
"
.
format
(
addr
[
0
]))
print
(
addr
)
conn
.
sendall
(
b
"
Hello, welcome to this workshop
"
)
conn
.
close
()
start_chat_thread
(
conn
,
addr
)
s
.
close
()
\ No newline at end of file
This diff is collapsed.
Click to expand it.
client.py
View file @
8060e292
...
...
@@ -15,13 +15,13 @@ s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s
.
setsockopt
(
socket
.
SOL_SOCKET
,
socket
.
SO_REUSEADDR
,
1
)
s
.
connect
((
HOST
,
PORT
))
print
(
s
.
recv
(
2048
).
decode
())
while
(
True
):
msg
=
input
(
"
Message:
"
)
if
(
msg
==
"
quit
"
):
break
msg
=
name
+
"
:
"
+
msg
s
.
send
(
msg
.
encode
())
s
.
send
all
(
msg
.
encode
())
s
.
close
()
This diff is collapsed.
Click to expand it.
log.txt
0 → 100755
View file @
8060e292
This diff is collapsed.
Click to expand it.
mqtt-chat-npy.py
0 → 100755
View file @
8060e292
#!/usr/bin/env python3
import
paho.mqtt.client
as
mqtt
from
threading
import
Thread
import
npyscreen
HOST
=
'
broker.hivemq.com
'
PORT
=
1883
TIMEOUT
=
60
f
=
None
TOPIC
=
'
lahtp-sockets-group
'
class
SubThread
(
Thread
):
def
__init__
(
self
):
Thread
.
__init__
(
self
)
class
ChatForm
(
npyscreen
.
SplitForm
,
npyscreen
.
ActionForm
):
def
create
(
self
):
client
.
connect
(
HOST
,
PORT
,
TIMEOUT
)
self
.
draw_line_at
=
18
self
.
MOVE_LINE_ON_RESIZE
=
True
self
.
chat_screen
=
self
.
add
(
npyscreen
.
MultiLine
,
name
=
"
MultiLineChatScreen
"
,
height
=
18
)
self
.
chat_screen
.
display
()
self
.
message_field
=
self
.
add
(
npyscreen
.
TitleText
,
name
=
"
Send:
"
,
editable
=
True
)
def
on_ok
(
self
):
#npyscreen.notify_confirm(self.message_field.value)
client
.
publish
(
TOPIC
,
self
.
message_field
.
value
)
self
.
message_field
.
value
=
""
def
on_cancel
(
self
):
exit
(
0
)
def
add_msg
(
message
):
if
(
self
.
chat_screen
.
value
is
None
):
self
.
chat_screen
.
value
=
message
else
:
self
.
chat_screen
.
value
=
self
.
chat_screen
.
value
+
"
\n
"
+
message
self
.
chat_screen
.
display
()
def
on_connect
(
client
,
userdata
,
flags
,
rc
):
npyscreen
.
notify
(
"
Connection Established
"
,
title
=
"
Info
"
)
def
on_message
(
client
,
userdata
,
message
):
f
=
open
(
"
log.txt
"
,
"
a
"
)
f
.
write
(
message
.
payload
)
#npyscreen.notify(message, title="New Message")
if
f
is
not
None
:
f
.
add_msg
(
message
.
payload
)
class
App
(
npyscreen
.
NPSAppManaged
):
def
onStart
(
self
):
f
=
self
.
addForm
(
"
MAIN
"
,
ChatForm
)
if
__name__
==
"
__main__
"
:
client
=
mqtt
.
Client
()
client
.
on_connect
=
on_connect
client
.
on_message
=
on_message
#f.chat_screen.value = "Welcome.\n"
app
=
App
()
app
.
run
()
\ No newline at end of file
This diff is collapsed.
Click to expand it.
mqtt_basics.py
0 → 100644
View file @
8060e292
#!/usr/bin/env python3
import
paho.mqtt.client
as
mqtt
HOST
=
'
broker.hivemq.com
'
PORT
=
1883
TIMEOUT
=
60
def
on_message
(
client
,
userdata
,
message
):
print
(
"
{}: {}
"
.
format
(
message
.
topic
,
str
(
message
.
payload
)))
def
on_connect
(
client
,
userdata
,
flags
,
rc
):
print
(
"
Connected.
"
)
client
.
subscribe
(
'
lahtp-sockets-group
'
)
client
=
mqtt
.
Client
()
client
.
on_message
=
on_message
client
.
on_connect
=
on_connect
client
.
connect
(
HOST
,
PORT
,
TIMEOUT
)
client
.
loop_forever
()
This diff is collapsed.
Click to expand it.