Skip to content

Commit

Permalink
socket server start
Browse files Browse the repository at this point in the history
  • Loading branch information
leconize committed Apr 4, 2018
1 parent 72ab79a commit dafeea1
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 0 deletions.
12 changes: 12 additions & 0 deletions socket_server/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Requirement for project
- the project will be able to receive data from mobile phones by socket server.
- The socket server will be TCP server if it's possible
- There will be two type of data coming from the phone.The first type is images of web page user are watching and the second is the position of eye gaze of the user.
- the format of the image data will be jpeg because it will cause lesser data space and improve load time.
- database of this project is PostgreSQL.

# Socket Expected Behavior
- After connection success. Will request for the image of the webpage.
- after getting the image of the webpage the server will save data in the filesystem in folder ./img and save path to the database.database will generate ID with auto increment and send back to the socket server.
- socket server would reply id of the image in the database to mobile if all of the operations succeeded.
- after initialization complete mobile will send gaze point(x, y) along with the id of the image to ensure that gaze point.
Empty file added socket_server/src/__init__.py
Empty file.
37 changes: 37 additions & 0 deletions socket_server/src/server.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import asyncio
import uvloop

class Server():

def __init__(self, host, port):
self.host = host
self.port = port

async def handle_client(self, reader, writer):
writer.close()
return

def start(self, loop):
# socket_server = socket.socket()
# socket_server.bind((self.host, self.port))
# socket_server.listen(5)
# socket_server.setblocking(False)
coro = asyncio.start_server(self.handle_client, self.host, self.port, loop=loop)
self.server = loop.run_until_complete(coro)

def close(self):
self.server.close()
self.server


if __name__ == "__main__":
asyncio.set_event_loop_policy(uvloop.EventLoopPolicy())
server = Server("127.0.0.1", 8888)
loop = asyncio.get_event_loop()
server.start(loop)
try:
loop.run_forever()
except KeyboardInterrupt:
server.close()
loop.run_until_complete(server.server.wait_closed())
loop.close()
12 changes: 12 additions & 0 deletions socket_server/src/util.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import socket
def is_server_running(host, port):
args = socket.getaddrinfo(host, port, socket.AF_INET, socket.SOCK_STREAM)
for family, socktype, proto, _, sockaddr in args:
s = socket.socket(family, socktype, proto)
try:
s.connect(sockaddr)
except socket.error:
return False
else:
s.close()
return True
Empty file added socket_server/test/__init__.py
Empty file.
32 changes: 32 additions & 0 deletions socket_server/test/test_server.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import unittest
import asyncio
import uvloop

from src.util import is_server_running
from src.server import Server


class TestSocketServer(unittest.TestCase):

def setUp(self):
self.host = "127.0.0.1"
self.port = 8888
self.server = Server(self.host, self.port)
self.loop = asyncio.get_event_loop()
self.server.start(self.loop)

def test_init_server(self):
self.assertEqual(self.server.host, "127.0.0.1")
self.assertEqual(self.server.port, 8888)

def test_server_is_running(self):
self.assertTrue(is_server_running(self.host, self.port))

def tearDown(self):
self.server.close()
if self.loop.is_running():
self.loop.close()
# self.loop.close()

if __name__ == '__main__':
unittest.main()

0 comments on commit dafeea1

Please sign in to comment.