Python Socket ±à³Ì
client:
import socket, sys
if __name__ == '__main__':
#´¦Àí²ÎÊý
argv = sys.argv
if (len(argv)!=3) or (len(argv)==2 and argv[1]=='/?'):
print '>>>Useage:', argv[0], '<address> <port>'
sys.exit(0)
HOST = argv[1] #Server IPµØÖ·
PORT = int(argv[2]) #Server ¶Ë¿Ú
print '>>>HOST:', HOST
print '>>>PORT:', PORT
#´ò¿ªsocket
try:
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
except socket.error:
print '>>>Fail to open socket!'
sys.exit(1)
print '>>>Socket opened...'
#Á¬½Ó
try:
sock.connect((HOST, PORT))
except socket.error:
print '>>>Fail to connect!'
sock.close()
print '>>>Socket closed...'
sys.exit(1)
print '>>>Socket connected...'
flag = 1 #Ñ»·±êÖ¾
iReturnValue = 0 #ϵͳ·µ»ØÖµ
#ÏòServer·¢ËÍÐÅÏ¢²¢»ØÏÔ
while(flag):
msg = raw_input('>>>Input message: ')
if msg == '':
&
Ïà¹ØÎĵµ£º
¼Ìǰƪ¡¶Import Module¡·£¨http://blog.csdn.net/xiadasong007/archive/2009/09/02/4512797.aspx£©£¬¼ÌÐø·ÖÎöǶÈ벿·Ö»ù´¡ÖªÊ¶¡£Õâ´Î²»¶à˵£¬ÓÐʲôÎÊÌâ¼ÇµÃ¶à²éÓ¢ÎÄ×ÊÁÏ£¬¹úÄÚµÄÕâ·½Ãæ֪ʶÉÙ
»¹ÊÇÀ´¿´´úÂ룬дÍêÎÒ¾Í˯¾õÁË~
#include "python/python.h"
#include <iostream>
using namespace std;
int ......
¸Õ¸ÕдÍêPythonǶÈ벿·ÖµÄ¼òµ¥Àý×Ó£¨²î²»¶à¹»ÏÖÔÚÓõÄÀ²~£©£¬½Ó×Å¿´µãʵ¼ÊµÄ¶«Î÷£¬Èç¹ûûÓÐÕâЩӦÓõĻ°£¬Ç°ÃæµÄǶÈëҲûÓÐʲôÒâÒ塣ǶÈëµÄÆäËû²¿·ÖÒÔºóÓöµ½ÔÙд£¬²»±ØÒ»ÏÂ×Ó°ÑÄÇЩº¯Êý¶¼Åª¶®£¬ÊÇ°É~
OK£¬À´¿´Python¿âÖÐÎÒÈÏΪ×îºÃÍæµÄÒ»²¿·Ö£¬Ò²¾ÍÊÇPython¶ÔÍøÒ³µÄ²Ù×÷¡£
Õâƪ¼òµ¥ËµÏÂÈçºÎͨ¹ýÍøÖ·ÏÂÔØÍøÒ³£¬Ç°ÌᵱȻÊÇ ......
×î½üÔÚ¹«Ë¾¸ºÔðÒ»¸öÏîÄ¿£¬ÊÇ×öÒ»¸ö±àÒëÆ÷£¬´ó¼Ò¿ÉÄÜÖªµÀ£¬×ö±àÒëÆ÷Ò»°ãÓÃC++»òjava£¬µ«ÊÇÎÒµÄÕâ¸öÏîĿȴʹÓÃÁËpythonÀ´×öÕâ¸ö±àÒëÆ÷£¬ºÜÓÐÌôÕ½ÐÔ¡£
ÎÒ½ñÌìËù½²µÄÊÇÔÚ¿ª·¢¹ý³ÌÖУ¬¶ÔʹÓÃpython2.6ÓïÑԵĸÐÊÜ£¬Ä¿Ç°Õâ¸öÏîÄ¿ÒѾÍê³ÉÈý·ÖÖ®Ò»ÁË¡£
˵ʵ»°£¬python²¢²»ÊʺÏ×öÕâÑùµÄÏîÄ¿¡££¨ËäȻҲÄÜ×ö£©ÒÔÏÂÊÇ×ܽáÁËpythonÏà¹Ø ......
1. ´òÓ¡±äÁ¿ºÍ±äÁ¿×ÔÏÔ
>>> myString = 'Hello World!'
>>> print myString
Hello World!
>>> myString
'Hello World!'
ÒòΪ£º print Óï¾äµ÷ÓÃstr()º¯ÊýÏÔʾ¶ÔÏ󣬶ø½»»¥Ê½½âÊÍÆ÷Ôòµ÷ÓÃrepr()º¯ÊýÀ´ÏÔʾ¶ÔÏó
sys.stdout.write('hello')²»»áÔÚĩβ¼ÓÉÏ'\n'£¬¶øprint»á
2. ´òÓ¡Îļþ
hand ......
ѧϰpython±¾¾ÍÊÇÒµÓàÖ®ºÃ£¡Ê鱾ɶµÄ£¬¶¼ÎÞ´Ó˵Æð¡£ËùÒÔż¶ûÅöµ½Ò»¸öºÃµÄÄ£¿é£¬¾ÍÒªÔÚ²©¿ÍÖмǼһÏ£¬ÒÔ±ãÈÕºóÖ®Óá£ÊÇÒÔ¸ßÊÖÖ±½ÓµÄÆ®¹ý¡£ÏÂÃæÇÐÈëÕýÌâ¡£
globÊÇpython×Ô¼º´øµÄÒ»¸öÎļþ²Ù×÷Ïà¹ØÄ£¿é£¬ÄÚÈÝÒ²²»¶à£¬ÓÃËü¿ÉÒÔ²éÕÒ·ûºÏ×Ô¼ºÄ¿µÄµÄÎļþ£¬¾ÍÀàËÆÓÚWindowsϵÄÎļþËÑË÷£¬¶øÇÒÒ²Ö§³ÖͨÅä·û£¬*,?,[]Õ ......