Archives for category: Uncategorized
import socket
import sys,time,datetime,string, os

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
#localhost port 5555
s.bind(('',5555))
s.listen(1)

#http header actions
firstheader ='HTTP/1.0 200 OK\r\nServer: Custom\r\nDate:   \r\nContent-Type: text/html\r\nConnection: Closed\r\n\r\n'
imageheader ='HTTP/1.1 200 OK\r\nx-content-type-options:nosniff\r\nContent-Type: image/jpeg\r\nContent-Length: 59383\r\nConnection: Keep-Alive\r\n\r\n'

#NOTE: all print statements show up only on server *console*
#determine if file exists or not
def isfileexist(fname):
  if os.path.isfile(fn) == False:
      return False
  else:
      return True

def buildresponse(fname):
    #get length of file in bites

    #split the extension
    sfex = fname.split('.')
    ex = sfex[1]
    if ex == 'jpg':
       h = 'image/jpeg'
    if ex == 'png':
       h = 'image/png'
    if ex == 'gif':
       h = 'image/gif'
    if ex == 'html':
       h = 'text/html'

    if ismediafile(fname)== True:
      i = open(fname,'rb').read()
      bytelen = len(i)
      k = 'Content-Length: '+str(bytelen)
    else:
      k = ''

    strheader ='HTTP/1.1 200 OK\r\nx-content-type-options:nosniff\r\nContent-Type: '+str(h)+ '\r\n'+k+'\r\nConnection: Keep-Alive\r\n\r\n'
    return strheader

def ismediafile(fname):
    sfex = fname.split('.')
    ex = sfex[1]
    if ex == 'jpg':
       return True
    elif ex == 'png':
       return True
    elif ex == 'gif':
       return True
    else:
       return False

while True:

      conn, addr = s.accept()

      data = conn.recv(1024)

      #print data
      #split up parts of the contents in list/array
      r = data.split()
      f = r[1]
      #split / from url
      fn = f[1:len(f)]

      print "output"+fn
      #normal http GET resquests
      if r[0] == 'GET':
      #get file from GET header

         #print isfileexist(f)

         #if no defaultfile dish out index file
         if f == '/':
             u = open('index.html','r').read()
             conn.send(firstheader+str(u))
         else:
           #if file exists, dish out error file
           if isfileexist(f) == False:
               #trigger error file
               u = open('error.html','r').read()
               conn.send(firstheader+str(u))

           #if file does exist, open the file and pipe it to user
           if isfileexist(f) == True:
              #print ">>>>>>>>>>"+buildresponse(fn)

              if ismediafile(fn) == True:
                  u = open(fn,'rb').read()
                  conn.send(buildresponse(fn)+str(u))
              else:
                   u = open(fn,'r').read()
                   conn.send(firstheader+str(u))

           #######################
           #shuts server down
           ########################
           if f == '/shutdown.html':

              print "server has been shutdown"
              sys.exit()
              break

      #handle post requests, when someone submits a form
      if r[0] == 'POST':

         a = r.index("Content-Length:")+2
         qs = r[a]

         #put POST query into a dictionary
         d = dict([x.split("=") for x in qs.split("&")])

         #print out the variables from post
         #print "USERNAME: "+ d['user']
         #print "PASSWORD: "+ d['pass']

         u = open(fn,'r').read()
         conn.send(firstheader+str(u))

      conn.close()
      print "connect closed...still listening"

First test for sending mouse controls over a network
mouse controls are captured using the win32api module
and sent over udp socket thru the network using IP 192.168.2.100 a commmon
network ip gateway.

Client code as stated below

import win32api
import socket
import time
import json

UDPSock = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
# Simply set up a target address and port ...
addr = ("192.168.2.100",50002)

def formatdata(mevent,curx,cury):
    #s = "mouse event {0}, x {1}, y{2}".format(mevent,curx,cury)
    s = json.dumps({mevent: (curx,cury)})
    UDPSock.sendto(s,addr)


old = (0, 0)
while True:
      coord = win32api.GetCursorPos()
      x = coord[0]
      y = coord[1]

      if(x,y) != old:
               #print "do something x{0} , y{1}".format(x,y)
               formatdata("move",x,y)
               old = (x,y)
      else:
               old

      #works
      a=win32api.GetAsyncKeyState(0X01)
      b=win32api.GetAsyncKeyState(0x02)
      if (a == -32768):
           formatdata("lclk",x,y)
      if (b == -32768):
         formatdata("rclk",x,y)

SERVER CODE

import win32api,win32con
import time
import sys
import json
import socket

UDPSock = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)

listen_addr = ("192.168.2.100",50002)
UDPSock.bind(listen_addr)

while True:
      data = UDPSock.recvfrom(1024)
      s = data[0]
      y  = json.loads(s)


      for a,c in y.iteritems():
          event = "{0}".format(a)
          x,y = c[0],c[1]
          if event=="move":
             win32api.SetCursorPos(( int(round(x)),int(round(y))))
          if event=="lclk":
             win32api.SetCursorPos(( int(round(x)),int(round(y))))
             win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN,x,y,0,0)
             win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP,x,y,0,0)
          if event=="rclk":
             win32api.SetCursorPos(( int(round(x)),int(round(y))))
             win32api.mouse_event(win32con.MOUSEEVENTF_RIGHTDOWN,x,y,0,0)
             win32api.mouse_event(win32con.MOUSEEVENTF_RIGHTUP,x,y,0,0)
          #print event,x,y

      if data[0]=="exit":

         print "server shutdown"
         sys.exit()

issues

1) lack of authorization, client and server just assuming they are the only ones to be talking to each other, there should be a handshaking routines.
2) client may have different resolution from server, this mean mouse movements on the server side will be restricted to clients monitor resolution
3) mouse buttons do not work correctly