#!/usr/bin/python

import os, socket, struct, sys

WRITE_HEADER         = "III"
WRITE_HEADER_LEN     = struct.calcsize(WRITE_HEADER)
READ_HEADER        = "IIII"
READ_HEADER_LEN    = struct.calcsize(READ_HEADER)
HOST = 'localhost'
PORT = 12345

def pack_message(arg):
    size = WRITE_HEADER_LEN + len(arg)
    stream = struct.pack(WRITE_HEADER + "%ds" % len(arg),
             socket.htonl(1), socket.htonl(3), socket.htonl(size), arg)
    return stream

def main():
    print ' Note:'
    print ' **************************************************** '
    print ' Pls make sure #python VirtIOChannel.py open in the guest first '
    print ' pls input the unix socket device '
    print ' eg:your CLI:-chardev socket,id=channel2,path=/tmp/helloworld2 ,then input "/tmp/helloworld2" here '
    print ' **************************************************** ' 
#    path = raw_input("pls input the unix socket address which you directed to\n")
#    print ' the path is ' + sys.argv[1]
#    vport = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
#    vport.connect(sys.argv[1])
    vport = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    vport.connect((HOST, PORT))

    ff = open("a.txt")
    arg = ff.read(65535)
    stream = pack_message(arg)
    vport.send(stream)

if __name__ == "__main__":
    main()
