Last active
August 29, 2015 13:58
-
-
Save grejppi/9958631 to your computer and use it in GitHub Desktop.
originally a server-client test but it became something that generates POETRY
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
import socket | |
import string | |
import random | |
import time | |
import urllib.request | |
import json | |
def findwords(title): | |
f = urllib.request.urlopen('http://en.wiktionary.org/w/api.php?action=query&list=categorymembers&cmtitle={cat}&cmsort=timestamp&cmdir=desc&cmlimit=500&format=json'.format(cat=title)) | |
js = json.loads(f.read().decode()) | |
return [v['title'] for v in js['query']['categorymembers']] | |
parts = [findwords(t) for t in [ | |
'Category:English_nouns', | |
'Category:English_third-person_singular_forms', | |
'Category:English_plurals', | |
'Category:English_conjunctions' | |
]] | |
HOST = 'localhost' | |
PORT = 31337 | |
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
s.connect((HOST, PORT)) | |
def randomsentence(): | |
global parts | |
s = [] | |
p = 0 | |
while p == 0 or random.randint(0, 3) != 0: | |
s += [parts[p % len(parts)][random.randint(0, len(parts[p % len(parts)]) - 1)]] | |
p += 1 | |
ret = ' '.join(s) + '.' | |
return ret[0].upper() + ret[1:] | |
while True: | |
time.sleep(random.randint(1, 4)) | |
s.send(randomsentence().encode()) | |
s.close() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
import socket | |
import string | |
HOST = 'localhost' | |
PORT = 31337 | |
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
s.bind((HOST, PORT)) | |
s.listen(1) | |
conn, addr = s.accept() | |
print(addr) | |
try: | |
while True: | |
data = conn.recv(1024).decode() | |
print(data) | |
conn.send(b'200 WHATEVER') | |
except: | |
pass | |
conn.close() | |
s.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment