Skip to content

Instantly share code, notes, and snippets.

@monk1337
Created January 11, 2018 06:47
Show Gist options
  • Save monk1337/332b34a26be1327a1efa7393d56c0b00 to your computer and use it in GitHub Desktop.
Save monk1337/332b34a26be1327a1efa7393d56c0b00 to your computer and use it in GitHub Desktop.
import socket
import threading
from newsapi.articles import Articles
import pickle
import sys
options = ['news', 'jokes', 'life_style']
a=Articles('cff9541f3b5a4558957e63ea59b3743b')
class ThreadedServer(object):
dict_cache={}
def __init__(self, host, port):
self.host = host
self.port = port
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
self.sock.bind((self.host, self.port))
def listen(self):
self.sock.listen(5)
while True:
client, address = self.sock.accept()
print("client post is {}".format(address[1]))
self.dict_cache['client']=address[1]
print(self.dict_cache)
client.settimeout(3600)
threading.Thread(target = self.listenToClient,args = (client,address)).start()
def listenToClient(self, client, address):
size = 1000000
while True:
try:
data_99 = pickle.dumps(options)
client.send(data_99) #1
data = client.recv(size) #2
if data:
da_1= str(data,'utf-8')
if da_1 == 'news':
client.send(pickle.dumps([k for i in a.get_by_popular(source='techcrunch')['articles'] for k in i]))
client.send(b'Which option would you like to select : description , author , url , urlToImage , title, publishedAt >>> ')
data_2 = client.recv(size)
client.send(pickle.dumps([i[str(data_2,'utf-8')] for i in a.get_by_popular(source='techcrunch')['articles']]))
client.send(b"would you like to quit ? yes | No ")
data_3 = client.recv(size)
if str(data_3,'utf-8') == 'yes':
break
else:
raise Exception('Client disconnected')
except:
client.close()
return False
while True:
port_num = input("Port? ")
try:
port_num = int(port_num)
break
except ValueError:
pass
ThreadedServer('', port_num).listen()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment