如何創建一個在網絡上工作的套接字服務器?
這僅僅是一個正確的端口轉發問題,還是我必須購買域名?
如果我需要一個域,是否有任何自由的方式來做到這一點?
背景
我目前工作在Windows 7(32位)使用Python 2.7.3基本IM項目。
我想用控制檯將消息從控制檯發送到我朋友的電腦之一。 This非常寶貴,但它只能在一臺機器內運行。
在我試圖通過互聯網發送消息時,我嘗試過使用幾種策略。
因爲我最終會用我的Mac作爲服務器,我已經使用Port Map到端口轉發不同的端口。當我做到這一點,巨蟒給予我這個錯誤提示信息:
Enter the PORT number (1 - 10,000)4235 Socket Created Bind failed. Error Code : 49 Message Can't assign requested address Traceback (most recent call last): File "/Users/BigKids/Desktop/Server v3.py", line 18, in <module> sys.exit() SystemExit
我試圖簡單地把我的IP地址作爲端口,但它顯示錯誤信息:
Please enter the host's IP >>> 68.***.***.128 Enter the PORT number (1 - 10,000)2432 Socket Created Bind failed. Error Code : 10049 Message The requested address is not valid in its context Traceback (most recent call last): File "D:\Python Programs\Sockets\First Project\Server v3.py", line 19, in <module> sys.exit() SystemExit
這是必要的代碼:
HOST = raw_input("Please enter the host's IP >>> ") PORT = input ("Enter the PORT number (1 - 10,000)") s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) print "Socket Created" try: s.bind((HOST, PORT)) except socket.error, msg: print "Bind failed. Error Code : " + str(msg[0]) + " Message " + str(msg[1]) sys.exit()
您的局域網是互聯網的一個子集。同樣的原則適用 - 你需要一個IP連接到端口來監聽/發送數據。 – Makoto