1

If anyone can help me, that would be great. I created a python server and client script on Linux with python version 2.6. To my understanding as long as I install the same version of python in Windows, I should be able to run the same script with out a problem. I did this and the script doesn't do anything except print out "Importerror: no module named fcntl". I'm adding the server code and the client. If any one can shed some light on what it is that I'm doing wrong, that would be great. As well, since I'm already here, how can I possibly also get the client side code to run automatically in windows with out having to install python in client side machines.

CLIENT

  #!/usr/bin/env python

import socket
import fcntl
import struct

#############################
def get_ip_address(ifname):
    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    return socket.inet_ntoa(fcntl.ioctl(
        s.fileno(),
        0x8915,  # SIOCGIFADDR
        struct.pack('256s', ifname[:15])
    )[20:24])

def getHwAddr(ifname):
    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    info = fcntl.ioctl(s.fileno(), 0x8927,  struct.pack('256s', ifname[:15]))
    return ''.join(['%02x:' % ord(char) for char in info[18:24]])[:-1]

host = socket.gethostname()

#print host
#print getHwAddr('eth0')
#print get_ip_address(enter code here'eth0')
info = "<"+get_ip_address('eth0')+">","{"+ getHwAddr('eth0')+"}","~"+host+"~"
############################

TCP_IP = 'IP'
TCP_PORT = xxxx
BUFFER_SIZE = 1024
MESSAGE = "Hello, World!"

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((TCP_IP, TCP_PORT))
s.send(str(info))
data = s.recv(BUFFER_SIZE)
s.close()

#print "received data:", data

SERVER

import socket
import re
import subprocess

TCP_IP = ''
TCP_PORT = XXXX
BUFFER_SIZE = 2048  # Normally 1024, but we want fast response

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((TCP_IP, TCP_PORT))
s.listen(50)

conn, addr = s.accept()
print 'Connection address:', addr
while 1:
     data = conn.recv(BUFFER_SIZE)
     if not data: break
     print "received data:", data
     da = open("data.txt", "wb")
     da.write(data);
     conn.send(data)  # echo
subprocess.Popen(["python", "ReadandInsert.py"])
conn.close()

Also, quick mention. The server side code will only run in Linux and the client code will be running in Windows. Thank you again for anyone that can lend a hand...!!

Cesar
  • 17
  • 1
  • 5
  • If you notice in the [fcntl documentation](https://docs.python.org/2/library/fcntl.html), it only works on _Unix_. While _python_ works across many different platforms, various modules (or even functions/classes within a module) in the standard library rely on OS specific behavior and are provided only if the OS supports that functionality. – mgilson Jun 02 '14 at 03:19

1 Answers1

0

fcntl only exists on Unix platforms.

Try using the following to get the mac address on Windows in replace of your fcntl.ioctl() call:

import netifaces as nif
...
...
def getHwAddr(ip):
    'Returns a list of MACs for interfaces that have given IP, returns None if not found'
    for i in nif.interfaces():
        addrs = nif.ifaddresses(i)
        try:
            if_mac = addrs[nif.AF_LINK][0]['addr']
            if_ip = addrs[nif.AF_INET][0]['addr']
        except IndexError: 
            if_mac = if_ip = None
        except KeyError:
            if_mac = if_ip = None
        print if_ip, ip
        if if_ip == ip:
            return if_mac
    return None

This is from a user name @kursancew, but I think it is very appropriate. I modified the name to match.

Then, just fix this line so you can configure by IP instead of by interface name across the board (interface names in Windows are a bit ... longer).

ip_address = '123.234.34.5'    # from your configuration
...
...
info = "<"+ip_address+">","{"+ getHwAddr(ip_address)+"}","~"+host+"~"
Community
  • 1
  • 1
woot
  • 7,406
  • 2
  • 36
  • 55
  • Ok, I've removed fcntl and I'm trying to use netifaces but when I try to import it I get the following error. `>>> import netifaces Traceback (most recent call last): File "", line 1, in import netifaces ImportError: No module named 'netifaces'` As well, this is the python I'm using in windows 7, 32 bit. `Python 3.4.1 (v3.4.1:c0e311e010fc, May 18 2014, 10:38:22) [MSC v.1600 32 bit (Intel)] on win32 Type "copyright", "credits" or "license()" for more information.` @kursancew – Cesar Jun 03 '14 at 03:17
  • You'll have to pip install it. It's a third party module. – woot Jun 03 '14 at 03:24
  • I've been trying with no avail. Installed Python 3,2.7,2.6 just to try to get pip to work and nothing. Might there be anything with having XP as the machine or it shouldn't have anything to do? – Cesar Jun 03 '14 at 19:40
  • Nevermind, I'm was trying to insert into python, sorry and thank you for the help. – Cesar Jun 03 '14 at 20:05