Ò׽ؽØͼÈí¼þ¡¢µ¥Îļþ¡¢Ãâ°²×°¡¢´¿ÂÌÉ«¡¢½ö160KB

python »ñÈ¡±¾»úip

>>> from socket import socket, SOCK_DGRAM, AF_INET
>>> s = socket(AF_INET, SOCK_DGRAM)
>>> s.connect(('google.com', 0))
>>> s.getsockname()
('192.168.1.113', 43711) 
Linux:
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])
#get_ip_address('lo')»·»ØµØÖ·
#get_ip_address('eth0')Ö÷»úipµØÖ· 
Windows:
import re,urllib2
from subprocess import Popen, PIPE
print "±¾»úµÄ˽ÍøIPµØַΪ£º" + re.search('\d+\.\d+\.\d+\.\d+',Popen('ipconfig', stdout=PIPE).stdout.read()).group(0)
print "±¾»úµÄ¹«ÍøIPµØַΪ£º" + re.search('\d+\.\d+\.\d+\.\d+',urllib2.urlopen("http://www.whereismyip.com").read()).group(0) 


Ïà¹ØÎĵµ£º

¡¶Dive Into Python¡··ÇËÀ²»¿É

ÒëÕßÑÔ£º
ÔçÔÚ 2008 Äê 8 Ô£¬ÎÒ¾ÍÔøÔÚ×Ô¼ºµÄ²©¿Í·¢±íÁËһƪ¡¶ÎªÊ²Ã´<Dive into Python>²»ÖµµÃÍƼö¡·£¨http://blog.csdn.net/lanphaday/archive/2008/08/28/2845258.aspx
£©£¬µ±Ê±ÒýÆðµÄÌÖÂ۾Ͳ»¶à˵ÁË£¬²»¹ýÊÂʵÉϵ½½ñÌìÈÔÈ»ÓÐÐí¶àÅóÓÑÁôÑÔÓëÎÒÌÖÂÛ£¬ÈÃÎÒ¼¸´ÎÔôÐIJ»ËÀ£¬ÏëдÔÙÉîÈëÅúÅС£ºÃÔ˵ÄʱºòÔÚÎÒÕæÕýÔÜ×㶯Á ......

python¶ÁдÎļþ


1.open
ʹÓÃopen´ò¿ªÎļþºóÒ»¶¨Òª¼ÇµÃµ÷ÓÃÎļþ¶ÔÏóµÄclose()·½·¨¡£±ÈÈç¿ÉÒÔÓÃtry/finallyÓï¾äÀ´È·±£×îºóÄܹرÕÎļþ¡£
file_object = open('thefile.txt')
try:
     all_the_text = file_object.read( )
finally:
     file_object.close( )
×¢£º²»Ä ......

pythonÖÐpackage»úÖƵÄÁ½ÖÖʵÏÖ·½Ê½


µ±Ö´ÐÐimport
moduleʱ£¬½âÊÍÆ÷»á¸ù¾ÝÏÂÃæµÄËÑË÷·¾¶£¬ËÑË÷module1.pyÎļþ¡£
1) µ±Ç°¹¤×÷Ŀ¼
2) PYTHONPATHÖеÄĿ¼
3) Python°²×°Ä¿Â¼
(/usr/local/lib/python)
ÊÂʵÉÏ£¬Ä£¿éËÑË÷ÊÇÔÚ±£´æÔÚsys.pathÕâ¸öÈ«¾Ö±äÁ¿ÖеÄĿ¼ÁбíÖнøÐÐËÑË÷¡£
sys.path»áÔÚ½âÊÍÆ÷¿ªÊ¼Ö´ÐÐʱ±»³õʼ»¯³É°üº¬£º
1)µ±Ç°¹¤×÷Ŀ¼
2) PYT ......

Python¡¢UnicodeºÍÖÐÎÄ[ת]


¹Ø¼ü×Ö: python
Python¡¢UnicodeºÍÖÐÎÄ[ת] 
pythonµÄÖÐÎÄÎÊÌâÒ»Ö±ÊÇÀ§ÈÅÐÂÊÖµÄÍ·ÌÛÎÊÌ⣬ÕâƪÎÄÕ½«¸øÄãÏêϸµØ½²½âÒ»ÏÂÕâ·½ÃæµÄ֪ʶ¡£µ±È»£¬¼¸ºõ¿ÉÒÔÈ·¶¨µÄÊÇ£¬ÔÚ½«À´µÄ°æ±¾ÖУ¬python»á³¹µ×½â¾ö´ËÎÊÌ⣬²»ÓÃÎÒÃÇÕâôÂé·³ÁË¡£ 
ÏÈÀ´¿´¿´pythonµÄ°æ±¾£º 
>>> import sys 
>> ......
© 2009 ej38.com All Rights Reserved. ¹ØÓÚE½¡ÍøÁªÏµÎÒÃÇ | Õ¾µãµØͼ | ¸ÓICP±¸09004571ºÅ