开发者

python sockets on Mac OS X gives AttributeError: 'module' object has no attribute 'IF_INET'

开发者 https://www.devze.com 2023-03-23 17:19 出处:网络
I am trying out some python network programming on Mac OS X 10.6. I am using Python 2.7 as installed by Homebrew, and am attempting to run a simple script from Foundations Of Python Network Programmin

I am trying out some python network programming on Mac OS X 10.6. I am using Python 2.7 as installed by Homebrew, and am attempting to run a simple script from Foundations Of Python Network Programming (great book btw) that contains a UDP client and server, but am getting the aforementioned AttribuiteError when trying to use varibles from the socket module. The script is as follows:

#! /开发者_StackOverflow中文版usr/bin/env python

import socket, sys

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

MAX = 65535
PORT = 1060

if sys.argv[1:] == ['server']:
    s.bind(('127.0.0.1', PORT))
    print('Listening at', s.getsockname())
    while True:
        data, address = s.recvfrom(MAX)
        print('The client at ', address, ' says ', repr(data))
        s.sendto('Your data was %d bytes' % len(data), address)

elif sys.argv[1:] == ['client']:
    print('address before sending: ', s.getsockname())
    s.sendto('This is my message', ('127.0.0.1', PORT))
    print('address after sending: ', s.getsockname())
    data, address = s.recvfrom(MAX)
    print('The server at ', address, ' says ', repr(data))
else:
    print >>sys.stderr, 'usage: udp_local.py client|server'

Running this script via python script.py server results in the following error:

Traceback (most recent call last):
  File "udp_local.py", line 5, in <module>
    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
AttributeError: 'module' object has no attribute 'AF_INET'

I have experimented with the python console, and after running import socket I can't seem to get any module variables. I have googled extensively and have yet to turn anything up with this same problem. Does anyone have any insight into what it might be? Just in case, here is the output of env python:

Python 2.7.1 (r271:86832, Jul 10 2011, 10:39:45) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin


I took a look at the Python source. On some platforms, AF_INET is defined in SOCKET.py or IN.py, which is automatically generated from /usr/include/sys/socket.h or something similar.

I couldn't actually see where this was defined for OSX, but it's possible something weird is going on in your version.

By far the most likely problem is that you've broken an important rule -- don't ever name a file after a module in the standard library. A socket.py file in your path or in the same directory as udp_local.py would cause errors like this. I recently did this with argparse while trying to answer a question here.

As a temporary workaround, you could try AF_INET = 2 and AF_INET = 1 -- it seems to be 2 in most places but may be 1 on non Unix platforms.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号