-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpython-socket-server.py
More file actions
118 lines (90 loc) · 2.84 KB
/
Copy pathpython-socket-server.py
File metadata and controls
118 lines (90 loc) · 2.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import socket
import select
import time
#http://forums.devshed.com/python-programming-11/a-few-socket-questions-75012.html
#http://davidf.sjsoft.com/mirrors/mcmillan-inc/sock1.html
#try assyncore
#create an INET, STREAMing socket
serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
#bind the socket to a public host,
# and a well-known port
port = 5555
serversocket.bind((socket.gethostname(), port)) #this is the one you want
#serversocket.bind(("127.0.0.1", port))
"""
to connect if you have a router
type ipconfig in dos and get your routers ip address
eg: 192.168.1.105
"""
#become a server socket
serversocket.listen(5)
serversocket.setblocking(0)
#serversocket.settimeout(10000);
#serversocket.setblocking(0)
#serversocket.settimeout(.01);
def handle_data(data):
if data[0:len("<policy-file-request/>")] == "<policy-file-request/>":
print socko.send('<?xml version=\"1.0\"?><cross-domain-policy><allow-access-from domain="*" to-ports= "*" /></cross-domain-policy>')
print "sent policy file"
closesocket(socko)
else:
print "you said" + data
connections = []
conn_info = [];
counter = 0;
print "going to listen on port " + str(port)
def closesocket(socko):
socko.close()
connections.remove(socko) #remove is pretty cool if it works!
socko = None
while 1:
"""counter = counter + 1
if counter == 1000:
counter = 0
print "reset"
for y in range(0,len(connections)):
print conn_info[y] """
time.sleep(.001) #change this in the future for speed
ready_to_read, ready_to_write, in_error = select.select([serversocket],[],[],0) #should I change that 0 to .001
#print len(ready_to_read)
for sock in ready_to_read:
print "test"
(clientsocket, address) = sock.accept()
#print clientsocket
#print address
print "putting " + address[0] + " onto connections";
conn_info.append(address)
clientsocket.setblocking(0)
connections.append(clientsocket)
print len(connections)
if len(connections) > 0:
to_read, to_write, to_error = select.select(connections,connections,connections,0)
for socko in to_read:
#i needed this try block only for google chrome! wierd.
try:
data = socko.recv(1024) #1024 potential problem causer
except socket.error, msg:
closesocket(socko)
break
print data
if not data:
print "no data"
closesocket(socko)
break
try:
#print "this is the first" + data[0]
#for flash policy stuff
#socko.send(data)
for sockwrite in to_write:
print "test"
ret = handle_data(data)
if ret != None:
sockwrite.send(ret)
#print socko.send("HTTP/1.0 200 OK\r\n")
#print socko.send("Content-Type: text/html\r\n")
#socko.send("Content-Length: 16\r\n\r\nThis is the text")
print "i sent output"
except socket.error, msg:
closesocket(socko)
for socko in to_error:
print socko + " had an error"