Skip to content

Commit cc6b494

Browse files
author
Nico Colic
committed
added sample script to show how query a stanford server
1 parent 8686e3a commit cc6b494

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

ask_stanford_server.py

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import socket
2+
import argparse , sys
3+
4+
HOSTNAME = 'localhost'
5+
PORT = 2020
6+
7+
def setup(host,port):
8+
s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
9+
s.connect((host,port))
10+
return s
11+
12+
def send(socket_,message):
13+
if not isinstance(message, bytes):
14+
try:
15+
message = message.encode()
16+
except Exception as e:
17+
print(e)
18+
return
19+
20+
socket_.sendall(message)
21+
socket_.shutdown(socket.SHUT_WR)
22+
23+
def get_reply(socket_,expected=1024):
24+
reply = ""
25+
while True:
26+
data = socket_.recv(expected)
27+
if data == b'':
28+
break
29+
reply += data.decode()
30+
31+
return reply.strip()
32+
33+
if __name__ == '__main__':
34+
parser = argparse.ArgumentParser()
35+
parser.add_argument('message',action="store")
36+
arguments = parser.parse_args(sys.argv[1:])
37+
38+
s = setup(HOSTNAME,PORT)
39+
send(s,arguments.message)
40+
r = get_reply(s)
41+
print(r)
42+

0 commit comments

Comments
 (0)