免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2550 | 回复: 0
打印 上一主题 下一主题

C++调python写的socket server 调不起来 求高手指教啊 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-09-30 11:38 |只看该作者 |倒序浏览
本帖最后由 zengbo1019 于 2011-09-30 11:40 编辑

==================================================================================================
C++代码,python2.7.2,windows xp,vs2008,调用python  socketserver,为什么调不起来啊
==============================================================================================
#include "stdafx.h"
#include <iostream>
#include "Python.h"

using namespace::std;


int _tmain(int argc, _TCHAR* argv[])
{
    PyObject *pDict,*pyFile,*pyScriptFile;
    Py_Initialize();
    //PyRun_SimpleString("print 'hello,world'");
   
    pyFile=PyFile_FromString("C://Python27//socketserver.py","r");
   
   
    FILE *fp=PyFile_AsFile(pyFile);  
   
    pDict=PyDict_New();   
    PyDict_SetItemString(pDict,"__builtins__",PyEval_GetBuiltins());
   
    pyScriptFile=PyRun_File(fp,"C://Python27//socketserver.py",Py_file_input,pDict, pDict);
    if(NULL == pyScriptFile){
        //fclose(fp);
        Py_DECREF(pyFile);
        Py_DECREF(pDict);
    }
    //fclose(fp);
    //Py_DECREF(pyScriptFile);
    //Py_DECREF(pyFile);
    //Py_DECREF(pDict);

    Py_Finalize();
    //cin.get();
        return 0;
}
================================================================================
socketserver.py
===============================================================================
# -*- coding: cp936 -*-
import socket
import threading
import SocketServer

class ThreadedTCPRequestHandler(SocketServer.BaseRequestHandler):

    def handle(self):
        #print 'connected from %s'%self.client_address[0]
        while 1:
            try:
                data = self.request.recv(1024)
            except socket.error as ex:
                break
            if not data:
                break
            cur_thread = threading.currentThread()
            response = "%s: %s" % (cur_thread.getName(), data)
            self.request.send(response)

class ThreadedTCPServer(SocketServer.ThreadingMixIn , SocketServer.TCPServer):
    pass

def startSocketServer():
    # Port 0 means to select an arbitrary unused port
    HOST, PORT = "localhost", 8888

    server = ThreadedTCPServer((HOST, PORT), ThreadedTCPRequestHandler)
    ip, port = server.server_address

    # Start a thread with the server -- that thread will then start one
    # more thread for each request
    server_thread = threading.Thread(target=server.serve_forever)
    # Exit the server thread when the main thread terminates
    server_thread.setDaemon(True)
    server_thread.start()
    print "Server loop running in thread:", server_thread.getName()
#if __name__ == "__main__":
   
startSocketServer()
===========================================================
运行结果
==========================================================


=============================================================
18713 refs]
请按任意键继续. . .
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP