Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
tubessister
IF3230-Tugas-Besar-Sister-2015
Commits
565e7736
Commit
565e7736
authored
Apr 20, 2015
by
Tony
Browse files
Update login+signup, databasenya ga tau kesimpen di mana si mongo
parent
6265fa9f
Changes
2
Hide whitespace changes
Inline
Side-by-side
src/client/client.py
View file @
565e7736
...
...
@@ -2,6 +2,14 @@
import
socket
#for sockets
import
sys
#for exit
import
hashlib
from
hashlib
import
md5
#md5 hash function
def
md5
(
string
):
m
=
hashlib
.
md5
()
m
.
update
(
string
.
encode
(
'UTF-8'
))
return
m
.
hexdigest
()
#create an INET, STREAMing socket
try
:
...
...
@@ -14,7 +22,7 @@ print ('Socket Created')
host
=
'127.0.0.1'
;
port
=
5005
;
try
:
remote_ip
=
socket
.
gethostbyname
(
host
)
...
...
@@ -27,20 +35,37 @@ except socket.gaierror:
s
.
connect
((
remote_ip
,
port
))
print
(
'Socket Connected to '
+
host
+
' on ip '
+
remote_ip
)
user_token
=
''
#Send some data to remote server
user_input
=
input
(
'Masukkan angka 1-15 (0 untuk keluar):'
)
#Server Status#
if
user_input
==
'1'
:
message
=
'{
\"
method
\"
:
\"
serverStatus
\"
,
\"
server
\"
: [{
\"
ip
\"
:
\"
167.205.32.46
\"
,
\"
port
\"
: 8000}]}'
#Sign Up#
elif
user_input
==
'2'
:
message
=
'{
\"
method
\"
:
\"
signup
\"
,
\"
username
\"
:
\"
williamstefan
\"
,
\"
password
\"
:
\"
1234
\"
}'
username
=
input
(
"Username: "
)
password
=
input
(
"Password: "
)
message
=
'{"method": "signup", "username": "'
+
username
+
'", "password": "'
+
md5
(
password
)
+
'"}'
#Login#
elif
user_input
==
'3'
:
message
=
'{
\"
method
\"
:
\"
login
\"
,
\"
username
\"
:
\"
williamstefan
\"
,
\"
password
\"
:
\"
1234
\"
}'
username
=
input
(
"Username: "
)
password
=
input
(
"Password: "
)
message
=
'{"method": "login", "username": "'
+
username
+
'", "password": "'
+
md5
(
password
)
+
'"}'
#response from server
user_token
=
'55335e89fc0a81d87f582ed8'
#Inventory#
elif
user_input
==
'4'
:
message
=
'{
\"
method
\"
:
\"
inventory
\"
,
\"
token
\"
:
\"
1
\"
}'
message
=
'{"method": "inventory", "token": "55335e89fc0a81d87f582ed8"}'
#MixItem#
elif
user_input
==
'5'
:
message
=
'{
\"\
"
:
\
"
mixitem
\
"
,
\
"
token
\
"
:
\"
1
\
"
,
\
"
item1
\
"
: 0,
\
"
item2
\
"
: 1}'
message
=
'{
"method
": "mixitem", "token":
"55335e89fc0a81d87f582ed8
", "item1": 0, "item2": 1}'
elif
user_input
==
'6'
:
message
=
'{
\"
method
\"
:
\"
map
\"
,
\"
token
\"
:
\"
1
\"
}'
elif
user_input
==
'7'
:
...
...
@@ -60,7 +85,9 @@ elif user_input == '13':
elif
user_input
==
'14'
:
message
=
'{
\"
method
\"
:
\"
fetch
\"
,
\"
token
\"
:
\"
1
\"
,
\"
offer_token
\"
: 1}'
elif
user_input
==
'15'
:
message
=
'{
\"
method
\"
:
\"
canceloffer
\"
,
\"
token
\"
:
\"
1
\"
,
\"
offer_token
\"
: 1}'
message
=
'{
\"
method
\"
:
\"
canceloffer
\"
,
\"
token
\"
:
\"
1
\"
,
\"
offer_token
\"
: 1}'
elif
user_input
==
'16'
:
message
=
''
;
try
:
#Set the whole string
...
...
src/server/server.py
View file @
565e7736
...
...
@@ -2,30 +2,34 @@
import
json
import
socket
import
sys
import
time
import
_thread
from
_thread
import
*
HOST
=
'127.0.0.1'
# Symbolic name meaning all available interfaces
PORT
=
5005
# Arbitrary non-privileged port
s
=
socket
.
socket
(
socket
.
AF_INET
,
socket
.
SOCK_STREAM
)
print
(
'Socket created'
)
import
pymongo
from
pymongo
import
MongoClient
#Bind socket to local host and port
try
:
s
.
bind
((
HOST
,
PORT
))
except
(
socket
.
error
,
msg
):
print
(
'Bind failed. Error Code : '
+
str
(
msg
[
0
])
+
' Message '
+
msg
[
1
])
sys
.
exit
()
#Pymongo MongoDB Setup
client
=
MongoClient
()
client
=
MongoClient
(
'localhost'
,
27017
)
print
(
'Socket bind complete'
)
db
=
client
.
gamesister
user
=
db
.
user
map
=
db
.
map
tradebox
=
db
.
tradebox
#Start listening on socket
s
.
listen
(
10
)
print
(
'Socket now listening'
)
#Function for handling tracker connection.
#def trackerThread(conn):
#while True:
#try:
#Receiving from tracker
#json_input = conn.recv(1024)
#if not json_input:
# break;
#else:
#Function for handling connections. This will be used to create threads
def
client
t
hread
(
conn
):
def
client
T
hread
(
conn
):
#Sending message to connected client
conn
.
send
(
'Welcome to the server. Type something and hit enter
\n
'
.
encode
(
'utf-8'
))
#send only takes string
...
...
@@ -35,14 +39,58 @@ def clientthread(conn):
try
:
#Receiving from client
json_input
=
conn
.
recv
(
1024
)
reply
=
'OK...'
+
json_input
.
decode
(
'UTF-8'
)
if
not
json_input
:
break
else
:
print
(
'json_input'
,
json_input
.
decode
(
'UTF-8'
))
decoded
=
json
.
loads
(
json_input
.
decode
(
'UTF-8'
))
print
(
"JSON parsing example: "
,
decoded
[
'method'
])
#print ("JSON parsing example: ", decoded['method'])
userQuery
=
''
userInventory
=
[]
#Sign Up
if
(
decoded
[
'method'
]
==
"signup"
):
query
=
user
.
find
({
"username"
:
decoded
[
'username'
].
lower
()})
if
(
query
.
count
()
==
0
):
#user available
data
=
{
"username"
:
decoded
[
'username'
].
lower
(),
"password"
:
decoded
[
'password'
],
"inventory"
:
{
"0"
:
2
,
"1"
:
2
,
"2"
:
2
,
"3"
:
2
,
"4"
:
0
,
"5"
:
0
,
"6"
:
0
,
"7"
:
0
,
"8"
:
0
,
"9"
:
0
},
"position"
:
{
"x"
:
0
,
"y"
:
0
},
"map_token"
:
"5533621ffc0a81d87f582edb"
}
user
.
insert
(
data
)
reply
=
{
"status"
:
"ok"
}
else
:
#user exist
print
(
"User already exist"
)
reply
=
{
"status"
:
"fail"
,
"description"
:
"username exists"
}
#Login
elif
(
decoded
[
'method'
]
==
"login"
):
userQuery
=
user
.
find_one
({
"username"
:
decoded
[
'username'
],
"password"
:
decoded
[
'password'
]})
if
(
userQuery
==
None
):
#username || password not match
print
(
"LOGIN FAILED"
)
reply
=
{
"status"
:
"fail"
,
"description"
:
"username/password combination is not found"
}
else
:
#username && password match
print
(
"LOGIN OK"
)
timestamp
=
int
(
time
.
time
())
reply
=
{
"status"
:
"ok"
,
"token"
:
str
(
userQuery
[
'_id'
]),
"x"
:
userQuery
[
'position'
][
'x'
],
"y"
:
userQuery
[
'position'
][
'y'
],
"time"
:
timestamp
}
#Inventory
elif
(
decoded
[
'method'
]
==
'inventory'
):
for
(
item
,
n
)
in
userQuery
[
'inventory'
].
values
():
for
i
in
range
(
0
,
n
):
userInventory
.
append
(
item
)
reply
=
{
"status"
:
"ok"
,
"inventory"
:
userInventory
}
#MixItem
#elif (decoded['method'] == 'mixitem'):
print
(
reply
)
conn
.
sendall
(
reply
.
encode
(
'UTF-8'
))
except
Exception
as
e
:
...
...
@@ -51,6 +99,29 @@ def clientthread(conn):
#came out of loop
conn
.
close
()
### MAIN PROGRAM ###
# MAKING SOCKET CONNECTION #
HOST
=
''
# Symbolic name meaning all available interfaces
PORT
=
5005
# Arbitrary non-privileged port
s
=
socket
.
socket
(
socket
.
AF_INET
,
socket
.
SOCK_STREAM
)
print
(
'Socket created'
)
#Bind socket to local host and port
try
:
s
.
bind
((
HOST
,
PORT
))
except
(
socket
.
error
,
msg
):
print
(
'Bind failed. Error Code : '
+
str
(
msg
[
0
])
+
' Message '
+
msg
[
1
])
sys
.
exit
()
print
(
'Socket bind complete'
)
#Start listening on socket , 10 connect requests
s
.
listen
(
10
)
print
(
'Socket now listening'
)
#now keep talking with the client
while
1
:
#wait to accept a connection - blocking call
...
...
@@ -58,6 +129,6 @@ while 1:
print
(
'Connected with '
+
addr
[
0
]
+
':'
+
str
(
addr
[
1
]))
#start new thread takes 1st argument as a function name to be run, second is the tuple of arguments to the function.
start_new_thread
(
client
t
hread
,(
conn
,))
start_new_thread
(
client
T
hread
,(
conn
,))
s
.
close
()
\ No newline at end of file
Write
Preview
Supports
Markdown
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